These days, knowledge science tasks don’t finish with the proof of idea; each challenge has the aim of being utilized in manufacturing. It is crucial, subsequently, to ship high-quality code. I’ve been working as an information scientist for greater than ten years and I’ve observed that juniors often have a weak degree in growth, which is comprehensible, as a result of to be an information scientist that you must grasp math, statistics, algorithmics, growth, and have data in operational growth. On this collection of articles, I wish to share some suggestions and good practices for managing knowledgeable knowledge science challenge in Python. From Python to Docker, with a detour to Git, I’ll current the instruments I take advantage of daily.
The opposite day, a colleague instructed me how he needed to reinstall Linux due to an incorrect manipulation with Python. He had restored an previous challenge that he wished to customise. On account of putting in and uninstalling packages and altering variations, his Linux-based Python surroundings was not practical: an incident that might simply have been prevented by establishing a digital surroundings. But it surely exhibits how necessary it’s to handle these environments. Happily, there may be now a superb instrument for this: uv.
The origin of those two letters is just not clear. In line with Zanie Blue (one of many creators):
“We thought-about a ton of names — it’s actually laborious to choose a reputation with out collisions today so each title was a steadiness of tradeoffs. uv was given to us on PyPI, is Astral-themed (i.e. ultraviolet or common), and is quick and straightforward to kind.”
Now, let’s go into just a little extra element about this excellent instrument.
Introduction
UV is a contemporary, minimalist Python tasks and packages supervisor. Developed solely in Rust, it has been designed to simplify Dependency Administration, digital surroundings creation and challenge group. UV has been designed to restrict frequent Python challenge issues similar to dependency conflicts and surroundings administration. It goals to supply a smoother, extra intuitive expertise than conventional instruments such because the pip + virtualenv combo or the Conda supervisor. It’s claimed to be 10 to 100 occasions sooner than conventional handlers.
Whether or not for small private tasks or growing Python purposes for manufacturing, UV is a strong and environment friendly answer for bundle administration.
Beginning with UV
Set up
To put in UV, if you’re utilizing Home windows, I like to recommend to make use of this command in a shell:
winget set up --id=astral-sh.uv -e
And, if you’re on Mac or Linux use the command:

To confirm appropriate set up, merely kind right into a terminal the next command:
uv model
Creation of a brand new Python challenge
Utilizing UV you’ll be able to create a brand new challenge by specifying the model of Python. To start out a brand new challenge, merely kind right into a terminal:
uv init --python x:xx project_name
python x:xx
should be changed by the specified model (e.g. python 3.12
). Should you shouldn’t have the required Python model, UV will maintain this and obtain the right model to begin the challenge.
This command creates and robotically initializes a Git repository named project_name. It incorporates a number of recordsdata:
- A
.gitignore
file. It lists the weather of the repository to be ignored within the git versioning (it’s primary and ought to be rewrite for a challenge able to deploy). - A
.python-version
file. It signifies the python model used within the challenge. - The
README.md
file. It has a goal to explain the challenge and explains methods to use it. - A
hi there.py
file. - The
pyproject.toml
file. This file incorporates all of the details about instruments used to construct the challenge. - The
uv.lock
file. It’s used to create the digital surroundings whenever you use uv to run the script (it may be in comparison with the requierements.txt)
Bundle set up
To put in new packages on this subsequent surroundings it’s important to use:
uv add package_name
When the add command is used for the primary time, UV creates a brand new digital surroundings within the present working listing and installs the required dependencies. A .venv/ listing seems. On subsequent runs, UV will use the prevailing digital surroundings and set up or replace solely the brand new packages requested. As well as, UV has a robust dependency resolver. When executing the add command, UV analyzes your complete dependency graph to discover a suitable set of bundle variations that meet all necessities (bundle model and Python model). Lastly, UV updates the pyproject.toml and uv.lock recordsdata after every add command.
To uninstall a bundle, kind the command:
uv take away package_name
It is vitally necessary to wash the unused bundle out of your surroundings. You need to preserve the dependency file as minimal as attainable. If a bundle is just not used or is not used, it should be deleted.
Run a Python script
Now, your repository is initiated, your packages are put in and your code is able to be examined. You may activate the created digital surroundings as typical, however it’s extra environment friendly to make use of the UV command run
:
uv run hi there.py
Utilizing the run command ensures that the script shall be executed within the digital surroundings of the challenge.
Handle the Python variations
It’s often really useful to make use of totally different Python variations. As talked about earlier than the introduction, it’s possible you’ll be engaged on an previous challenge that requires an previous Python model. And sometimes it is going to be too tough to replace the model.
uv python checklist
At any time, it’s attainable to alter the Python model of your challenge. To try this, it’s important to modify the road requires-python within the pyproject.toml
file.
As an illustration: requires-python = “>=3.9”
Then it’s important to synchronize your surroundings utilizing the command:
uv sync
The command first checks present Python installations. If the requested model is just not discovered, UV downloads and installs it. UV additionally creates a brand new digital surroundings within the challenge listing, changing the previous one.
However the brand new surroundings doesn’t have the required bundle. Thus, after a sync command, it’s important to kind:
uv pip set up -e .
Change from virtualenv to uv
If in case you have a Python challenge initiated with pip and virtualenv and want to use UV, nothing might be easier. If there isn’t any necessities file, that you must activate your digital surroundings after which retrieve the bundle + put in model.
pip freeze > necessities.txt
Then, it’s important to init the challenge with UV and set up the dependencies:
uv init .
uv pip set up -r necessities.txt

Use the instruments
UV presents the opportunity of utilizing instruments by way of the uv instrument command. Instruments are Python packages that present command interfaces for similar to ruff, pytests, mypy, and so forth. To put in a instrument, kind the command line:
uv instrument set up tool_name
However, a instrument can be utilized with out having been put in:
uv instrument run tool_name
For comfort, an alias was created: uvx, which is equal to uv instrument run. So, to run a instrument, simply kind:
uvx tool_name
Conclusion
UV is a robust and environment friendly Python bundle supervisor designed to supply quick dependency decision and set up. It considerably outperforms conventional instruments like pip or conda, making it a superb option to handle your Python tasks.
Whether or not you’re engaged on small scripts or massive tasks, I like to recommend you get into the behavior of utilizing UV. And imagine me, making an attempt it out means adopting it.
References
1 — UV documentation: https://docs.astral.sh/uv/
2 — UV GitHub repository: https://github.com/astral-sh/uv
3 — A terrific datacamp article: https://www.datacamp.com/tutorial/python-uv