UV - A modern python project and dependency manager
Table of Contents
Introduction
This tutorial will guide you through using UV, a modern Python project and dependency manager. We will cover its installation, initialization, adding dependencies, running commands, and more. UV streamlines Python packaging and project management, making it a valuable tool for developers.
Step 1: Installation
To get started with UV, you need to install it on your system. Follow these steps:
- Open your terminal or command prompt.
- Run the following command to install UV:
pip install uv
Tip: Ensure you have Python and pip installed on your system. You can verify this by running python --version
and pip --version
.
Step 2: Initialization
Once UV is installed, you need to initialize it in your project directory.
- Navigate to your project folder using the terminal.
- Run the initialization command:
uv init
This command creates a new uv
configuration file, which is essential for managing your project dependencies.
Step 3: Adding Dependencies
With UV initialized, you can start adding dependencies to your project.
-
Use the following command to add a dependency:
uv add <package-name>
Replace
<package-name>
with the name of the package you want to include. -
To add multiple packages at once, list them separated by spaces:
uv add package1 package2 package3
Common Pitfall: Make sure to specify the correct package names to avoid installation errors.
Step 4: Running Commands
UV allows you to run commands easily within your project.
-
To execute a command, use:
uv run <command>
Replace
<command>
with the command you want to run. -
You can also specify scripts defined in your UV configuration.
Tip: Check your uv
configuration file for predefined scripts that you can run directly.
Step 5: Installing Python
If you need to install a specific version of Python for your project, follow these steps:
- Use the official Python website or a package manager like
pyenv
to install the required version. - After installation, verify by running:
python --version
Step 6: Speed Test
To evaluate the performance of UV, you can run a speed test.
- Use the following command to initiate a speed test:
uv speed-test
This will provide insights into how fast UV handles package management compared to other tools.
Step 7: Using uvx
UV also includes a feature called uvx
, which provides additional functionalities.
- To access
uvx
, simply run:
This command will show you the various options available withuvx help
uvx
.
Tip: Explore the uvx
features for enhanced project management capabilities.
Conclusion
In this tutorial, we covered the essential steps to get started with UV, including installation, initialization, adding dependencies, and running commands. By utilizing UV, you can simplify your Python project management and streamline your development workflow. For further exploration, check out the UV documentation and experiment with its features in your projects.