For years, the first step in any Python project, big or small, has often been the same opening your terminal and typing something like
python -m venv venv
. This manually creating virtual environments has become so ingrained that it feels like a mandatory part of the Python development lifecycle. But what if I told you that in the near future, this step could become a thing of the past?
Inroduction of uv
An extremely fast Python package and project manager, written in Rust. uv is presented as a modern tool that aims to revolutionize the way you manage Python projects and their dependencies. It introduces a new ecosystem for Python project management that is designed to be more organized and efficient.
Uv features
uv provides essential features for Python development — from installing Python and hacking on simple scripts to working on large projects that support multiple Python versions and platforms. When you come from the JavaScript ecosystem, it is similar to npm.
uv's interface can be broken down into sections, which are usable independently or together.
-
Python versions
Installing and managing Python itself.
-
: Install Python versions.uv python install
-
: View available Python versions.uv python list
-
: Find an installed Python version.uv python find
-
: Uninstall a Python version.uv python uninstall
-
-
Scripts
Executing standalone Python scripts, e.g.,
.example.py
-
: Run a script.uv run
-
: Add a dependency to a scriptuv add --script
-
: Remove a dependency from a scriptuv remove --script
-
-
Projects
Creating and working on Python projects, i.e., with a
.pyproject.toml
-
: Create a new Python project.uv init
-
: Add a dependency to the project.uv add
-
: Remove a dependency from the project.uv remove
-
: Run a command in the project environment.uv run
-
: Build the project into distribution archives.uv build
-
Practical use case
One of the most significant shifts uv introduces is the automatic creation of virtual environments . Forget the manual steps of using venv or other tools. uv handles this for you the moment you run your first script in a project.
Let's dive into some practical examples to see uv in action.
Working with Python Projects: Imagine you're starting a new web project. Traditionally, you would do something like this:
python -m venv .venv source .venv/bin/activate # On macOS/Linux .venv\Scripts\activate # On Windows pip install <your_dependencies>
Working on Python Projects with
For larger Python projects, uv offers a project structure managed through a
pyproject.toml
file, which is similar to package.json
in JavaScript.
-
Initializing a Project :
To start a new project with uv, go to your project directory in the terminal and run:
uv init <project_name>
For example:
uv init example-app
The project includes a
, a sample file (pyproject.toml
), a readme, and a Python version pin file (main.py
)..python-version
The
includes basic metadata. It does not include a build system, it is not a package and will not be installed into the environment:pyproject.toml
# pyproject.toml [project] name = "example-app" version = "0.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.13" dependencies = []
The sample file defines a
function with some standard boilerplate:main
# main.py def main(): print("Hello from example-app!") if __name__ == "__main__": main()
Python files can be executed with
after run this cmdcd example-app <project_name>
:uv run main.py
# output Hello from example-app!
-
Adding Dependencies :
To add dependencies to your project, you can update the
list in thedependencies
file. This is similar to adding packages in apyproject.toml
file in JavaScript. Once you have added your dependencies, make sure to run the appropriate command to install them in your environment.package.json
use the
command followed by the package names.uv add
uv add fastapi
This command not only installs the fastapi package and its dependencies but also automatically updates your pyproject.toml file to include fastapi under the [tool.uv.dependencies] section. For example, after adding fastapi, your pyproject.toml might look like this:
[project] name = "example-app" version = "0.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.13" dependencies = [ "fastapi>=0.115.12", # added fastapi package sucessfully ]
A
file (similar touv.lock
) is also created to ensure consistent dependency versions across different environments. You generally shouldn't edit this file directly.package-lock.json
Example Code with FastAPI :
from typing import Union from fastapi import FastAPI import uvicorn def main(): print("Hello from uv-python-no-create-venv!") uvicorn.run("main:app", port=5000, log_level="info") app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: Union[str, None] = None): return {"item_id": item_id, "q": q} if __name__ == "__main__": main()
To run this FastAPI application (assuming you have uvicorn installed, which you can add using
uv add uvicorn
), you can use a command like:
uv run main.py --app main:app --reload
Building a Distributable Package Using uv build
uv build
To distribute your project to others (e.g. to upload it to an index like PyPI), you'll need to build it into a distributable format.
Python projects are typically distributed as both source distributions (sdists) and binary distributions (wheels). The former is typically a
.tar.gz
or .zip
file containing the project's source code along with some additional metadata, while the latter is a .whl
file containing pre-built artifacts that can be installed directly.
uv build ls dist/
You can build the project in a different directory by providing a path to
uv build
, e.g., uv build path/to/project
.
uv build
will first build a source distribution, and then build a binary distribution (wheel) from that source distribution.
Conclusion
In summary, uv revolutionizes Python project management by automating virtual environments and simplifying dependency handling, making development more efficient and organized.
If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more content on Python, Fastapi , JavaScript, React.js, Next.js and other web Development and AI topics.
For Paid collaboration, Web Development, AI Agent freelancing work, mail me at: krishdesai044@gmail.com
Connect with me on Twitter, LinkedIn, and GitHub.
Thank you for Reading
Happy Coding