Image by Author
Â
uv is a fast Python package and project manager, written in Rust. It can be used instead of pip, pip-tools, pipx, poetry, pyenv, and virtualenv tools. uv provides a complete ecosystem for managing Python projects, allowing you to add dependencies, manage tools, run scripts, and more. In short, from installing Python to building Python applications, uv makes the process faster and easier. One of the best features of uv is its compatibility with the pip tool. This means that you just need to add “uv” in front of your command and all of your previous commands will work out of the box.Â
In this tutorial, we will learn how to install uv and explore its various features. We will create a project, add dependencies, run scripts, use tools, and also learn about its pip interface.
Â
Installing uv
Â
You can run the following command in the terminal to install uv in Linux and MacOS.
$ curl -LsSf https://astral.sh/uv/install.sh | sh
Â
For Windows, try using the Winget tool.
$ winget install --id=astral-sh.uv -e
Â
It is so flexible that you can install it using the pip interface.Â
Â
After the installation is completed, run the `uv` command in the terminal to check whether it is properly installed.
Â
Projects
Â
We will now initialize the Python project called “KDN.”
Â
Change the directory to the project.Â
$ cd /datasets/_deepnote_work/KDN
Â
As you can see, the project has all the necessary files.
Â
Â
We will run the sample Python file using the `run` command.Â
Â
As we can see, it first created the virtual environment folder and then ran the Python file.Â
Creating virtual environment at: .venv
Hello from kdn!
Â
We will proceed with installing and adding ‘pandas’ as a dependency to the project.Â
Â
Â
Scripts
Â
Now, we will learn how to run Python file more easily with uv. First, we will create a simple web scripting Python file with the necessary code.
$ echo "import requests; from bs4 import BeautifulSoup; response = requests.get('https://www.kdnuggets.com/author/abidali-awan'); soup = BeautifulSoup(response.content, 'html.parser'); print('Webpage Title:', soup.title.string)" > webscrape.py
Â
We will then add the dependencies to the web scripting file.
$ uv add --script webscrape.py "requests" "beautifulsoup4"
Â
These dependicing will be added to the file as a comment and only be read by the uv tool.
Â
 Â
When you run the Python file, it will first install the Python packages and then run the code.Â
Â
Output:
Installed 7 packages in 18ms
Webpage Title: Abid Ali Awan - KDnuggets
Â
This is quite useful if you work outside the project and have only a few Python files to run.Â
Â
Tools
Â
We can install and use CLI tools using the `uv` command. In our case, we are installing the `huggingface_hub` tools, which allow us to pull and push files from Hugging Face repositories.
$ uv tool install huggingface_hub
Â
To view all the installed tools, we can run the following command. Â
Â
We have installed the tool, but as you can see, it has a different name, “huggingface-cli,” for running the command.
huggingface-hub v0.25.1
- huggingface-cli
Â
To run the tool with a different name from the package name, we will use the `–from` flag and provide it with the package name and the tool name. Then, we will log in to Hugging Face Hub using the API key.Â
$ uv tool run --from huggingface-hub huggingface-cli login --token $HUGGINGFACEHUB_API_TOKEN
Â
The tool works.Â
Your token has been saved to /root/.cache/huggingface/token
Login successful
Â
The pip Interface
Â
uv is fully compatible with the pip tool, meaning we can run all the pip commands using the uv tool. All you have to do is add `uv` at the start of your script.Â
We will install the `controlflow` package using the pip interface.
$ uv pip install controlflow -q
Â
Then, we will view its description using the `show` command.Â
$ uv pip show controlflow
Â
Name: controlflow
Version: 0.10.0
Location: /root/venv/lib/python3.9/site-packages
Requires: jinja2, langchain-anthropic, langchain-core, langchain-openai, markdownify, openai, prefect, pydantic-settings, textual, tiktoken, typer
Required-by:
Â
In the end, we will create the `requirements.txt` using the freeze command.
$ uv pip freeze >> requirements.txt
Â
Final Thoughts
Â
uv is suitable for beginners, portfolio projects, and production-ready projects. It is becoming increasingly popular, with many orchestration tools replacing pip with uv and encouraging users to make the switch to this superior tool.
In my opinion, uv is better than pip when dealing with large projects with hundreds of dependencies. However, for small projects, I believe that pip tool is still superior in terms of speed and efficiency.
In this tutorial, we have learned about the new Python project management tool. We have also explored its features with code examples such as Project, Scripts, Tools, and the Pip interface.
Â
Â
Abid Ali Awan (@1abidaliawan) is a certified data scientist professional who loves building machine learning models. Currently, he is focusing on content creation and writing technical blogs on machine learning and data science technologies. Abid holds a Master’s degree in technology management and a bachelor’s degree in telecommunication engineering. His vision is to build an AI product using a graph neural network for students struggling with mental illness.