Modern Python Development with pyproject.toml and UV

The Future of Dependency Management — No More requirements.txtHello everyone,I have been exploring open-source Python repositories, and one common trend I’ve noticed — especially in newer projects — is the adoption of pyproject.toml. Having primarily u…


This content originally appeared on Level Up Coding - Medium and was authored by Rahul Beniwal

The Future of Dependency Management — No More requirements.txt

Hello everyone,

I have been exploring open-source Python repositories, and one common trend I’ve noticed — especially in newer projects — is the adoption of pyproject.toml. Having primarily used requirements.txt, I initially wondered where all the dependencies were listed when trying to build a project locally. Later, I learned more about this shift, and I’d like to share my findings with you all in the hope that they will be helpful.

Traditionally, developers relied on requirements.txt for dependency management. However, the Python community is increasingly adopting pyproject.toml and setup.py for a more standardized and integrated approach. UV by Astral simplifies this process by using pyproject.toml as a central configuration file. This tutorial will walk you through setting up and using UV to streamline your project's lifecycle.

Image source: Unsplash

What is UV?

UV is a modern Python packaging tool that simplifies dependency management, packaging, and publishing by leveraging pyproject.toml. It’s designed to be fast, intuitive, and compatible with Python’s PEP 517 and PEP 518 standards. To learn more about UV, you can refer to 👇

Speeding Up Python Dependency Management: A 10x Improvement Guide

UV is pip-compliant so you use commands like pip install -r requirements.txt by uv pip install -r requirements.txt

Why Use pyproject.toml Over requirements.txt?

  • Unified Configuration: Combines metadata, dependencies, and build system requirements in one file.
  • Enhanced Dependency Management: Allows for more precise version control and optional dependencies (or dev dependencies).

Setting Up pyproject.toml with UV

You can refer above 10x improvement article to know how to install UV. Don’t be lazy uv is great and my daily use tool.

Initializing a New Project

To create a new project, simply use:

uv init -p3.12
  • Specifying the version is optional if 3.12 is not present uv will download it automatically.

This command will create an initial structure, including pyproject.toml:

.
├── hello.py
├── pyproject.toml
└── README.md

The uv init command populates pyproject.toml with basic details:

[project]
name = "uv-and-pyproject"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []

Managing Dependencies with UV

Creating Virtual Env

This step is independent and can be used in existing Python projects also.

uv venv -p3.12
Using CPython 3.12.8
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate

Adding a Dependency

After activating the env you can simply do

uv add numpy
Resolved 2 packages in 14ms
Installed 1 package in 61ms
+ numpy==2.2.2

It will also update pyproject.toml

[project]
name = "uv-and-pyproject"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"numpy>=2.2.2",
]

Adding Development Dependencies

Development dependencies can be added using the — dev flag:

uv add --dev pytest

This will add a new section [dependency-groups] with a dev subsection:

[dependency-groups]
dev = [
"pytest>=8.3.4",
]

Removing Dependency

uv remove pytest --group dev
[dependency-groups]
dev = []
💡If your project contains multiple Python files then place those inside src directory.

[Legacy] Using setup.py

While pyproject.toml is becoming the standard, setup.py is still widely used for defining package metadata and build instructions. Here’s how you can integrate it:

from setuptools import setup, find_packages

setup(
name="uv-and-pyproject",
version="0.1.0",
description="Add your description here",
packages=find_packages(),
install_requires=[
"numpy>=2.2.2",
],
extras_require={
"dev": [
"pytest>=8.3.4",
],
},
)

Building and Packaging with UV

What Does “Building” Mean?

In the context of Python projects, “building” refers to the process of preparing your code for distribution. This typically involves compiling your code into a format that can be easily installed and run on other systems. The build process can create source distributions (sdist) and/or binary distributions (wheel).

Building Your Project

UV can use either setup.py or pyproject.toml to build your project. Here’s how you can do it:

uv build
Successfully built dist/uv_and_pyproject-0.1.0.tar.gz
Successfully built dist/uv_and_pyproject-0.1.0-py3-none-any.whl

Publishing Your Package

Once your package is built, you can publish it to PyPI using:

uv publish

Advanced Features of UV

Dependency Locking

Generate a lock file for precise dependency management:

uv lock

⚠️ pyproject file must exist for uv lock

Install dependencies from the lockfile:

uv sync

I know it looks familiar to rye sync. uv is the successor torye.

Rye

Understanding pip install -e . and pip install .

pip install -e .
  • Purpose: This command installs your project in “editable” or “development” mode.
  • Functionality: When you use -e (short for — editable), it allows you to make changes to your source code and have those changes immediately reflected without needing to reinstall the package. This is particularly useful during development when you are frequently modifying your code.
  • Use Case: Ideal for developers actively working on a project who need to test changes quickly. It links the source code directory directly to the Python environment. Consider you are contributing to django and you make some change so validate those changes by writing test cases or some other way you can install django locally to test changed functionality.
pip install .
  • Purpose: This command installs your project in the standard way, as a regular package.
  • Functionality: It builds the package and installs it into the Python environment. This is similar to how you would install any other package from PyPI.
  • Use Case: Suitable for testing the installation process or when you want to use the package as a dependency in another project without modifying the source code.

FAQs

1. What is the difference between pyproject.toml and setup.py?

  • pyproject.toml is a modern configuration file introduced by PEP 518 to specify build system requirements and project metadata. It provides a unified way to manage dependencies and build configurations. setup.py, on the other hand, is a traditional script used by setuptools to define package metadata and build instructions. While setup.py is still widely used, pyproject.toml is increasingly preferred for its simplicity and compliance with modern standards.

2. Why should I use UV instead of traditional tools like pip and virtualenv?

  • UV offers a faster and more integrated approach to managing Python projects. It simplifies dependency management, virtual environment creation, and project initialization by using pyproject.toml. UV is designed to be intuitive and compliant with PEP 517/518, making it a great choice for modern Python development workflows.

3. How does pip install -e . differ from pip install . ?

  • pip install -e . installs your project in editable mode, allowing you to make changes to the source code and see them reflected immediately. This is ideal for development. pip install . installs the project in a standard, non-editable mode, which is useful for testing the installation process or deploying the package.

4. Can I use UV with existing projects that already have a setup.py?

  • Yes, UV can work with projects that have a setup.py. You can initialize a pyproject.toml alongside your setup.py to manage dependencies and build configurations. UV will use the information from both files to build and manage your project.

5. How do I publish my package to PyPI using UV?

  • To publish your package to PyPI using UV, first ensure your pyproject.toml and/or setup.py are correctly configured with the necessary metadata. Then, build your package using uv build and publish it with uv publish. Make sure you have your PyPI credentials configured before publishing.

6. What are the benefits of using pyproject.toml for dependency management?

  • pyproject.toml offers several benefits for dependency management, including a unified configuration file for project metadata and dependencies, compliance with PEP 517/518, and support for optional dependencies. It simplifies the management of complex dependency trees and is increasingly adopted by the Python community for its flexibility and standardization.

Conclusion

Thanks for reading till the end, Hope you found it helpful. Please leave 👏 and your feedback to encourage. Follow Rahul Beniwal for more.

You can check the complete source code here.


Modern Python Development with pyproject.toml and UV was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Rahul Beniwal


Print Share Comment Cite Upload Translate Updates
APA

Rahul Beniwal | Sciencx (2025-02-12T01:50:14+00:00) Modern Python Development with pyproject.toml and UV. Retrieved from https://www.scien.cx/2025/02/12/modern-python-development-with-pyproject-toml-and-uv/

MLA
" » Modern Python Development with pyproject.toml and UV." Rahul Beniwal | Sciencx - Wednesday February 12, 2025, https://www.scien.cx/2025/02/12/modern-python-development-with-pyproject-toml-and-uv/
HARVARD
Rahul Beniwal | Sciencx Wednesday February 12, 2025 » Modern Python Development with pyproject.toml and UV., viewed ,<https://www.scien.cx/2025/02/12/modern-python-development-with-pyproject-toml-and-uv/>
VANCOUVER
Rahul Beniwal | Sciencx - » Modern Python Development with pyproject.toml and UV. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/12/modern-python-development-with-pyproject-toml-and-uv/
CHICAGO
" » Modern Python Development with pyproject.toml and UV." Rahul Beniwal | Sciencx - Accessed . https://www.scien.cx/2025/02/12/modern-python-development-with-pyproject-toml-and-uv/
IEEE
" » Modern Python Development with pyproject.toml and UV." Rahul Beniwal | Sciencx [Online]. Available: https://www.scien.cx/2025/02/12/modern-python-development-with-pyproject-toml-and-uv/. [Accessed: ]
rf:citation
» Modern Python Development with pyproject.toml and UV | Rahul Beniwal | Sciencx | https://www.scien.cx/2025/02/12/modern-python-development-with-pyproject-toml-and-uv/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.