Python package managers let you install and manage dependencies – such as Nami, Pandas, and so on – from your terminal.
In this article, you will learn how to use uvextreme An extremely fast Python package manager.
Conditions
To get the most out of this tutorial, you’ll need:
Learn how to execute commands in your terminal,
Be familiar with basic Python development/scripting.
Table of Contents
Why should you use UV?
uv is a free and open source Python project management tool. written in rust, uv Fast and easy to use. It has become the standard package manager for modern Python development. You can also use it to manage your virtual environment, making it a good alternative for that pip And venv.
Speed ​​test It has been shown uv Faster than other popular package managers when it comes to installing dependencies.
How to install UV
to install uvjust execute the following command in your terminal:
For Linux/MacOS:
curl -LsSf | sh
For Windows:
powershell -ExecutionPolicy ByPass -c "irm | iex"
How to set up a project with UV
To start a new project with uvrun the following command:
uv init freecodecamp-project
This creates a folder /freecodecamp-project in your working directory with the following structure:
├── .gitignore
├── .python-version
├── README.md
├── main.py
└── pyproject.toml
README.md is a markdown file, which you can use to get information about your project (similar to a repository readme file on GitHub). pyproject.toml is the configuration file for your project. You can edit your project to change its name, version and description. It also keeps track of dependencies that you add to the project. last but not least, main.py is where you keep your Python code.
Commonly used UV commands
Before you run any uv commands, move your terminal to your project folder:
cd freecodecamp-project
How to add dependencies
To add a dependency (for example, numpy), run:
uv add numpy
uv add 'numpy==2.3.0'
This automatically adds a virtual environment and a .lock file with a new entry:
├── .venv
└── uv.lock
.lock The file keeps a record of the exact versions of your dependencies and the packages they require. You should never edit it directly.
How to remove dependencies
To remove the dependency, run:
uv remove numpy
It will also delete the corresponding entry .lock File
How to Run Python Code
To run your Python script, execute:
uv run main.py
It will run the code inside the created virtual environment with installed dependencies in one step! That is, you don’t need to explicitly activate the virtual environment and then run your code.
How to transfer your plan
Sometimes you want to move your project to another machine. Maybe you want to share it with colleagues, or deploy it on a production server. Getting your code to run on another machine has never been easier uv. You can simply copy your project folder to the destination environment and run the following command in the destination terminal:
uv sync --locked
This installs the exact version of your project’s dependencies!
How to add and run tools
Sometimes you may need tools like ruffa Python linter (code formatter), which you can add to your project like any other dependency:
uv add ruff
To run the installed tool, execute:
uv run ruff check
How to run tools without adding them to your project
Sometimes you don’t want to add tools to your dependencies. For example, because you only want to check once. uv Got you covered tool run. Just run:
uv tool run ruff check
uvx ruff check
This tool will run independently in an isolated environment.
How to manage multiple Python versions
If you have Python installed on your system, uv Will use it. Otherwise, it will automatically install the latest Python version when you execute Python code or create a virtual environment. uv. However, you can also use it to manage multiple Python versions:
uv python install
uv python install 3.11 3.12
uv python list
uv python pin 3.11
How to Migrate from PIP to UV
uv Works as a drop-in replacement for pipmeaning you can use normal pip Orders with uv. Let’s say you receive one requirements.txt To install file listed dependencies, you can run:
uv pip install -r requirements.txt
The result
You just learned how to use uv! First uvmanaging dependencies, virtual environments and versions of Python was notoriously cumbersome. As ML Engineer mljobs.ioI can safely say that uv Has been a game changer. Check out uv‘ Documents to know more.