A New Python Package Manager


uv: A New Python Package Manager
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.
 

uv: A New Python Package Manageruv: A New Python Package Manager

 
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. 

 

uv: A New Python Package Manageruv: A New Python Package Manager

 

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.

 

uv: A New Python Package Manageruv: A New Python Package Manager

   

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.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here