A New Python Package deal Supervisor
Picture by Creator
uv is a quick Python bundle and undertaking supervisor, written in Rust. It may be used as a substitute of pip, pip-tools, pipx, poetry, pyenv, and virtualenv instruments. uv gives a whole ecosystem for managing Python initiatives, permitting you so as to add dependencies, handle instruments, run scripts, and extra. Briefly, from putting in Python to constructing Python functions, uv makes the method quicker and simpler. Among the finest options of uv is its compatibility with the pip device. Which means you simply want so as to add “uv” in entrance of your command and all your earlier instructions will work out of the field.
On this tutorial, we are going to learn to set up uv and discover its numerous options. We are going to create a undertaking, add dependencies, run scripts, use instruments, and in addition study its pip interface.
Putting in uv
You may run the next command within the terminal to put in uv in Linux and MacOS.
$ curl -LsSf https://astral.sh/uv/set up.sh | sh
For Home windows, strive utilizing the Winget device.
$ winget set up --id=astral-sh.uv -e
It’s so versatile you can set up it utilizing the pip interface.
After the set up is accomplished, run the `uv` command within the terminal to test whether or not it’s correctly put in.
Initiatives
We are going to now initialize the Python undertaking referred to as “KDN.”
Change the listing to the undertaking.
$ cd /datasets/_deepnote_work/KDN
As you’ll be able to see, the undertaking has all the mandatory information.
We are going to run the pattern Python file utilizing the `run` command.
As we are able to see, it first created the digital setting folder after which ran the Python file.
Creating digital setting at: .venv
Howdy from kdn!
We are going to proceed with putting in and including ‘pandas’ as a dependency to the undertaking.
Scripts
Now, we are going to learn to run Python file extra simply with uv. First, we are going to create a easy internet scripting Python file with the mandatory code.
$ echo "import requests; from bs4 import BeautifulSoup; response = requests.get('https://www.kdnuggets.com/writer/abidali-awan'); soup = BeautifulSoup(response.content material, 'html.parser'); print('Webpage Title:', soup.title.string)" > webscrape.py
We are going to then add the dependencies to the net scripting file.
$ uv add --script webscrape.py "requests" "beautifulsoup4"
These dependicing will probably be added to the file as a remark and solely be learn by the uv device.
If you run the Python file, it can first set up the Python packages after which run the code.
Output:
Put in 7 packages in 18ms
Webpage Title: Abid Ali Awan - KDnuggets
That is fairly helpful in case you work outdoors the undertaking and have just a few Python information to run.
Instruments
We will set up and use CLI instruments utilizing the `uv` command. In our case, we’re putting in the `huggingface_hub` instruments, which permit us to drag and push information from Hugging Face repositories.
$ uv device set up huggingface_hub
To view all of the put in instruments, we are able to run the next command.
We’ve put in the device, however as you’ll be able to see, it has a distinct title, “huggingface-cli,” for working the command.
huggingface-hub v0.25.1
- huggingface-cli
To run the device with a distinct title from the bundle title, we are going to use the `–from` flag and supply it with the bundle title and the device title. Then, we are going to log in to Hugging Face Hub utilizing the API key.
$ uv device run --from huggingface-hub huggingface-cli login --token $HUGGINGFACEHUB_API_TOKEN
The device works.
Your token has been saved to /root/.cache/huggingface/token
Login profitable
The pip Interface
uv is absolutely appropriate with the pip device, which means we are able to run all of the pip instructions utilizing the uv device. All it’s important to do is add `uv` at first of your script.
We are going to set up the `controlflow` bundle utilizing the pip interface.
$ uv pip set up controlflow -q
Then, we are going to view its description utilizing the `present` command.
$ uv pip present controlflow
Title: controlflow
Model: 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:
Ultimately, we are going to create the `necessities.txt` utilizing the freeze command.
$ uv pip freeze >> necessities.txt
Ultimate Ideas
uv is appropriate for newbies, portfolio initiatives, and production-ready initiatives. It’s changing into more and more widespread, with many orchestration instruments changing pip with uv and inspiring customers to make the swap to this superior device.
In my view, uv is healthier than pip when coping with massive initiatives with tons of of dependencies. Nevertheless, for small initiatives, I consider that pip device remains to be superior by way of pace and effectivity.
On this tutorial, we have now realized in regards to the new Python undertaking administration device. We’ve additionally explored its options with code examples similar to Venture, Scripts, Instruments, and the Pip interface.
Abid Ali Awan (@1abidaliawan) is an authorized information scientist skilled who loves constructing machine studying fashions. At the moment, he’s specializing in content material creation and writing technical blogs on machine studying and information science applied sciences. Abid holds a Grasp’s diploma in know-how administration and a bachelor’s diploma in telecommunication engineering. His imaginative and prescient is to construct an AI product utilizing a graph neural community for college students fighting psychological sickness.