Using uv
Create projects the modern way
Get ready to learn about uv, a super-fast Python package installer and dependency resolver. It's an awesome tool for managing your Python projects efficiently.
Creating a new project
Start a new project by making a directory. This keeps your project files neat and organized.
mkdir my_new_project
cd my_new_projectNow, initialize uv in your new project. This sets up everything you need to manage your dependencies.
uv initUnderstanding pyproject.toml
When you initialize a project with uv, it creates a pyproject.toml file. This is where your project's settings and dependencies live.
# pyproject.toml
[project]
name = "my-new-project"
version = "0.1.0"
[tool.uv.sources]
[tool.uv.scripts]You'll define your project's name, version, and the packages it needs here. It's a central hub for all project-related configuration.
Adding packages
Let's add a package to your project. Most projects use a testing framework like pytest.
uv add pytestuv will download pytest and its dependencies. It also updates your pyproject.toml file automatically.
The lock file
uv creates a uv.lock file. This file records the exact version of every package your project uses.
You should always commit your uv.lock file to version control (like Git). This way, other developers can install the exact same environment.
Running your code
After adding packages, you'll want to run your Python scripts. uv handles the virtual environment for you.
uv run python your_script.pyThis command executes your_script.py within the project's isolated environment. No need to manually activate a virtual environment.
# your_script.py
import pytest
def greet():
return "Hello, uv!"
print(greet())Common uv commands
Here are some of the most useful uv commands you'll use regularly:
uv add <package>: Add a new package to your project.uv remove <package>: Remove an installed package.uv update: Update all installed packages to their latest compatible versions.uv run <command>: Execute a command within the project's virtual environment.
Working with existing projects
If you join an existing project that uses uv, getting started is easy. Clone the repository and install the dependencies.
git clone my-awesome-project
cd my-awesome-project
uv syncuv sync reads the uv.lock file and installs all listed packages with their exact versions. This guarantees you have the exact same setup as other developers.
Virtual environment details
uv automatically creates and manages a virtual environment inside your project directory. It's usually in a hidden folder, something like .venv.
uv run and other commands handle it for you.This isolation ensures that your project's dependencies don't conflict with other Python projects on your machine.
Tips and tricks
- Always use
uv addto install new packages, notpip install. - Remember to commit your
pyproject.tomlanduv.lockfiles. - If you encounter issues,
uv cleancan sometimes fix them by removing cached data.
For more advanced usage and configuration, check out the official uv documentation.
What's next?
You've successfully learned the basics of uv! You can now create new projects, add dependencies, and run your code with confidence.
Explore more Python libraries, build your first web application, or dive deeper into testing. uv will be there to help manage your project's needs.