These days, information science tasks don’t finish with the proof of idea; each challenge has the objective of being utilized in manufacturing. It is crucial, subsequently, to ship high-quality code. I’ve been working as a knowledge scientist for greater than ten years and I’ve seen that juniors often have a weak degree in improvement, which is comprehensible, as a result of to be a knowledge scientist it’s essential grasp math, statistics, algorithmics, improvement, and have data in operational improvement. On this sequence of articles, I want to share some suggestions and good practices for managing knowledgeable information science challenge in Python. From Python to Docker, with a detour to Git, I’ll current the instruments I take advantage of on daily basis.
The opposite day, a colleague informed me how he needed to reinstall Linux due to an incorrect manipulation with Python. He had restored an outdated challenge that he wished to customise. Because of putting in and uninstalling packages and altering variations, his Linux-based Python setting was now not useful: an incident that might simply have been prevented by establishing a digital setting. But it surely reveals how essential it’s to handle these environments. Thankfully, there may be now a superb software for this: uv.
The origin of those two letters shouldn’t be clear. In accordance 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 this present day so each identify was a stability of tradeoffs. uv was given to us on PyPI, is Astral-themed (i.e. ultraviolet or common), and is brief and simple to kind.”
Now, let’s go into a bit extra element about this excellent software.
Introduction
UV is a contemporary, minimalist Python tasks and packages supervisor. Developed fully in Rust, it has been designed to simplify Dependency Administration, digital setting creation and challenge group. UV has been designed to restrict widespread Python challenge issues reminiscent of dependency conflicts and setting 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 creating Python functions for manufacturing, UV is a strong and environment friendly resolution 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 may create a brand new challenge by specifying the model of Python. To begin a brand new challenge, merely kind right into a terminal:
uv init --python x:xx project_name
python x:xx
have to be changed by the specified model (e.g. python 3.12
). Should you do not need the required Python model, UV will care for this and obtain the proper model to begin the challenge.
This command creates and mechanically initializes a Git repository named project_name. It accommodates 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 function to explain the challenge and explains how you can use it. - A
whats up.py
file. - The
pyproject.toml
file. This file accommodates all of the details about instruments used to construct the challenge. - The
uv.lock
file. It’s used to create the digital setting whenever you use uv to run the script (it may be in comparison with the requierements.txt)
Package deal set up
To put in new packages on this subsequent setting you need to use:
uv add package_name
When the add command is used for the primary time, UV creates a brand new digital setting within the present working listing and installs the required dependencies. A .venv/ listing seems. On subsequent runs, UV will use the prevailing digital setting 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 the 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 essential to scrub the unused bundle out of your setting. You must maintain the dependency file as minimal as attainable. If a bundle shouldn’t be used or is now not used, it have to 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 possibly can activate the created digital setting as traditional, however it’s extra environment friendly to make use of the UV command run
:
uv run whats up.py
Utilizing the run command ensures that the script will likely be executed within the digital setting of the challenge.
Handle the Python variations
It’s often beneficial to make use of completely different Python variations. As talked about earlier than the introduction, you could be engaged on an outdated challenge that requires an outdated Python model. And infrequently it will likely be too tough to replace the model.
uv python checklist
At any time, it’s attainable to vary the Python model of your challenge. To try this, you need to modify the road requires-python within the pyproject.toml
file.
For example: requires-python = “>=3.9”
Then you need to synchronize your setting utilizing the command:
uv sync
The command first checks current Python installations. If the requested model shouldn’t be discovered, UV downloads and installs it. UV additionally creates a brand new digital setting within the challenge listing, changing the outdated one.
However the brand new setting doesn’t have the required bundle. Thus, after a sync command, you need to kind:
uv pip set up -e .
Swap from virtualenv to uv
When you’ve got a Python challenge initiated with pip and virtualenv and want to use UV, nothing could possibly be less complicated. If there is no such thing as a necessities file, it’s essential activate your digital setting after which retrieve the bundle + put in model.
pip freeze > necessities.txt
Then, you need 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 potential of utilizing instruments through the uv software command. Instruments are Python packages that present command interfaces for reminiscent of ruff, pytests, mypy, and many others. To put in a software, kind the command line:
uv software set up tool_name
However, a software can be utilized with out having been put in:
uv software run tool_name
For comfort, an alias was created: uvx, which is equal to uv software run. So, to run a software, 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 consider 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 — An incredible datacamp article: https://www.datacamp.com/tutorial/python-uv