Home Backend Development Python Tutorial UV as an alternative to Poetry

UV as an alternative to Poetry

Dec 03, 2024 pm 02:20 PM

There are somehow too many package managers for Python. This, of course, has an obvious reason - the paucity of functionality of pip, the built-in package manager. But still, the inner perfectionist wants a simple solution out of the box. Install Python - and you immediately get a fast and convenient package manager, and preferably a Python version manager. But instead you get pip. Some people, of course, actually use it, but still poetry, pdm, conda, pipenv or at least pip-tools are much more convenient.

Well, okay, it would seem that poetry is good for everyone. I use it myself in most projects. But installing dependencies starts to seem slow - especially when rebuilding the docker container. Plus there is a hassle with installing poetry itself or changing versions of python - you change the version through, for example, pyenv, and poetry gives an error when you try to recreate the environment. Although it is assumed that it can work with different versions of Python. This can be solved, of course, easily - by specifying the full path to the interpreter, but it’s still a crutch. As well as installing the same pyenv, and generally working with it. And there seems to be nothing more to control python versions.

And so, in February, an interesting solution appeared from the creators of Ruff. UV package manager written in Rast. Purely console-based, of course, and the syntax is very reminiscent of poetry. In terms of functionality, these are almost the same thing, but with a bunch of goodies and several times faster. The doc shows this diagram of the installation time for the same set of dependencies:

UV как альтернатива Poetry

The syntax is really similar to poetry. This is how, for example, a project is created (go straight to the directory):

uv init project
cd project
Copy after login

There we have this structure:

project
├── .python-version
├── hello.py
├── pyproject.toml
└── README.md
Copy after login

Dependencies, like poetry, are saved in the pyproject.toml config, the python version is saved in .python-version.

Let's create a virtual environment:

uv venv
Copy after login

Add SQLAlchemy depending on:

uv add sqlalchemy
Copy after login

Or we can add a specific version:

uv add sqlalchemy@2.0.32
Copy after login

Now let's delete:

uv remove sqlalchemy
Copy after login

Now sugar - python version management. Let's install 3.11.9 and create an environment with such an interpreter.

uv python install 3.11.9
uv venv --python 3.11.9
Copy after login

The Python version will be saved in .python-version, and you don’t need to point this out to the package manager every time you change the interpreter, because UV is the package manager. Moreover, you don’t even have to install the version manually, but immediately create an environment with the desired version: if it is not installed, then the UV itself will pull it, that is, you don’t have to think about it at all - well, it’s a thrill.

Like poetry, there is functionality for building and publishing packages on PyPI. You can build a container and publish a package using two commands:

uv build
uv publish
Copy after login

You can also use UV as a supervisor, and run scripts and applications using uv run

And the cherry on the cake is the docker image.

FROM ghcr.io/astral-sh/uv:python3.12

WORKDIR /app

RUN uv venv

CMD ["run", "app"]
Copy after login

You don’t need to pull the python image and install UVs there via pip, you can immediately pull the UV image and have fun. Plus there are a lot of other tricks there, but about this in the dock (which, by the way, is very clear) - those who need these chips in the dock will not be afraid to go into the dock.

In general, a really convenient tool and a good alternative to poetry. There is no UV support in any IDE yet, but it's a matter of time. It’s cool that there are so many features, I want the project to develop. Share this post and write what you use.

P.S. And you need to indulge in TGC: https://t.me/dmkjfss

The above is the detailed content of UV as an alternative to Poetry. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1237
24
Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

How Much Python Can You Learn in 2 Hours? How Much Python Can You Learn in 2 Hours? Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Exploring Its Primary Applications Python: Exploring Its Primary Applications Apr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

See all articles