Urgent.News

What's breaking now, across thousands of outlets.

Tech

If not Python venv, then what?

For years, python -m venv .venv has been the default answer to Python dependency management and isolation. It works, it’s built into Python and almost every tutorial assumes you’ll use it. But today, more and more developers are asking a question Do I actually need venv anymore? And the answer is: not always. This article explores what modern alternatives exist and when they make sense. Option 1:…

For years, the command python -m venv .venv has been the standard approach for handling Python dependencies and creating isolated environments. This built-in method is assumed in virtually every tutorial. However, recent developments have led many developers to question whether venv is still necessary. This article examines modern alternatives and the circumstances in which they are appropriate.

One promising tool is uv, a Rust-based Python package manager that prioritizes speed and simplicity. To use uv, install it with curl -LsSf https://astral.sh/uv/install.sh | sh. Then, initiate a project with uv init my_app and navigate to the project folder. The generated structure includes a .python-version file, a main.py file with a ready-to-run hello world application, and a pyproject.toml file.

Dependencies can be added using uv add requests, which automatically creates a .venv folder for package installation. Alternatively, Poetry has been a popular choice for Python development. It can be installed via curl -LsSf https://install.python-poetry.org | python3 - and used to create a new project with poetry new myapp. The resulting structure differs slightly from uv's, with a pyproject.toml file but no .venv folder. Dependencies are managed with poetry add requests and run with poetry run python src/myapp/main.py.

PDM offers another approach, implementing ideas from PEP 582. Instead of isolated virtual environments, PDM stores dependencies in a local __pypackages__ directory. Installation involves curl -LsSf https://pdm-project.org/install-pdm.py | python3 -. The pdm new myapp command prompts for several key details like Python interpreter selection and licensing.

A typical project structure includes .venv, .pdm-python, and pyproject.toml files. Dependencies are added using pdm add requests, which places them in the .venv folder. Notably, PDM switched to .venv as the default due to PEP 582's non-standardization. Finally, containerization is another route for application isolation, with dependencies handled at the container level rather than local virtual environments.

While these alternatives don't fully replace venv, they represent a shift in mindset for Python project management, focusing on projects as primary units rather than standalone virtual environments.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Monday 7 September →