Data Scientist: From School to Work, Part I

-


Nowadays, data science projects don’t end with the proof of concept; every project has the goal of getting used in production. It will be important, subsequently, to deliver high-quality code. I even have been working as a knowledge scientist for greater than ten years and I even have noticed that juniors often have a weak level in development, which is comprehensible, because to be a knowledge scientist it’s good to master math, statistics, algorithmics, development, and have knowledge in operational development. On this series of articles, I would love to share some suggestions and good practices for managing knowledgeable data science project in Python. From Python to Docker, with a detour to Git, I’ll present the tools I take advantage of daily.


The opposite day, a colleague told me how he needed to reinstall Linux due to an incorrect manipulation with Python. He had restored an old project that he desired to customize. In consequence of putting in and uninstalling packages and changing versions, his Linux-based Python environment was now not functional: an incident that might easily have been avoided by organising a virtual environment. But it surely shows how essential it’s to administer these environments. Fortunately, there may be now a superb tool for this: uv.
The origin of those two letters shouldn’t be clear. In keeping with Zanie Blue (considered one of the creators):

“We considered a of names — it’s really hard to select a reputation without collisions this present day so every name was a balance of tradeoffs. uv was given to us on PyPI, is Astral-themed (i.e. ultraviolet or universal), and is and straightforward to type.”

Now, let’s go into just a little more detail about this glorious tool.


Introduction

UV is a contemporary, minimalist Python projects and packages manager. Developed entirely in Rust, it has been designed to simplify Dependency Management, virtual environment creation and project organization. UV has been designed to limit common Python project problems reminiscent of dependency conflicts and environment management. It goals to supply a smoother, more intuitive experience than traditional tools reminiscent of the pip + virtualenv combo or the Conda manager. It’s claimed to be 10 to 100 times faster than traditional handlers.

Whether for small personal projects or developing Python applications for production, UV is a sturdy and efficient solution for package management. 


Starting with UV

Installation

To put in UV, should you are using Windows, I like to recommend to make use of this command in a shell:

winget install --id=astral-sh.uv  -e

And, should you are on Mac or Linux use the command:

To confirm correct installation, simply type right into a terminal the next command:

uv version

Creation of a brand new Python project

Using UV you possibly can create a brand new project by specifying the version of Python. To start out a brand new project, simply type right into a terminal:

uv init --python x:xx project_name

have to be replaced by the specified version (e.g. ). In case you wouldn’t have the required Python version, UV will deal with this and download the right version to begin the project.

This command creates and mechanically initializes a Git repository namedproject_nameIt accommodates several files:

  • A.gitignorefile. It lists the weather of the repository to be ignored within the git versioning (it’s basic and needs to be rewrite for a project able to deploy).
  • A.python-versionfile. It indicates the python version utilized in the project.
  • The README.md file. It has a purpose to explain the project and explains the right way to use it.
  • A hello.pyfile.
  • The pyproject.tomlfile. This file accommodates all the knowledge about tools used to construct the project.
  • The uv.lock file. It’s used to create the virtual environment while you use uv to run the script (it will probably be in comparison with the )

Package installation

To put in latest packages on this next environment you will have to make use of:

uv add package_name

When the command is used for the primary time, UV creates a brand new virtual environment in the present working directory and installs the required dependencies. A .venv/ directory appears. On subsequent runs, UV will use the prevailing virtual environment and install or update only the brand new packages requested. As well as, UV has a robust dependency resolver. When executing the command, UV analyzes your entire dependency graph to seek out a compatible set of package versions that meet all requirements (package version and Python version). Finally, UV updates the pyproject.toml and uv.lock files after each command.

To uninstall a package, type the command:

uv remove package_name

It is extremely essential to wash the unused package out of your environment. You may have to maintain the dependency file as minimal as possible. If a package shouldn’t be used or is not any longer used, it have to be deleted.

Run a Python script

Now, your repository is initiated, your packages are installed and your code is able to be tested. You may the created virtual environment as usual, but it surely is more efficient to make use of the UV command :

uv run hello.py

Using the run command guarantees that the script will probably be executed within the virtual environment of the project.


Manage the Python versions

It is often really helpful to make use of different Python versions. As mentioned before the introduction, you might be working on an old project that requires an old Python version. And infrequently it can be too difficult to update the version.

uv python list

At any time, it is feasible to vary the Python version of your project. To do this, you will have to switch the road within the pyproject.tomlfile.

As an example: requires-python = “>=3.9”

Then you will have to synchronize your environment using the command:

uv sync

The command first checks existing Python installations. If the requested version shouldn’t be found, UV downloads and installs it. UV also creates a brand new virtual environment within the project directory, replacing the old one.

But the brand new environment doesn’t have the required package. Thus, after a sync command, you will have to type:

uv pip install -e .

Switch from virtualenv to uv

If you will have a Python project initiated with pip and virtualenv and want to make use of UV, nothing may very well be simpler. If there is no such thing as a file, it’s good to activate your virtual environment after which retrieve the package + installed version.

pip freeze > requirements.txt

Then, you will have to init the project with UV and install the dependencies:

uv init .
uv pip install -r requirements.txt
Correspondence table between pip + virtualenv and UV, image by creator.

Use the tools

UV offers the potential for using tools via the command. Tools are Python packages that provide command interfaces for reminiscent of , , , etc. To put in a tool, type the command line:

uv tool install tool_name

But, a tool could be used without having been installed:

uv tool run tool_name

For convenience, an alias was created: , which is reminiscent of So, to run a tool, just type:

uvx tool_name

Conclusion

UV is a robust and efficient Python package manager designed to offer fast dependency resolution and installation. It significantly outperforms traditional tools like or making it a superb alternative to administer your Python projects.

Whether you’re working on small scripts or large projects, I like to recommend you get into the habit of using UV. And consider me, trying 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 important datacamp article: https://www.datacamp.com/tutorial/python-uv

ASK ANA

What are your thoughts on this topic?
Let us know in the comments below.

0 0 votes
Article Rating
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Share this article

Recent posts

0
Would love your thoughts, please comment.x
()
x