Python Virtual Environments: venv, pip, and requirements.txt Explained

Python virtual environments venv solve a problem every Python developer hits sooner or later: two projects need two different versions of the same package, and installing one breaks the other. A virtual environment is a separate folder with its own Python interpreter and its own installed packages, so each project gets exactly the dependencies it needs and nothing leaks between them. This guide shows how venv, pip, and requirements.txt fit together, and how to use them every day without thinking about them.

The habit to build is simple: one project, one environment, created before you install anything. It takes thirty seconds, and it prevents the kind of breakage that costs an afternoon. If you have ever run a script that worked yesterday and fails today after installing an unrelated package, a missing environment is usually the reason.

Why One Global Python Is Not Enough

Installing packages globally means every project on your machine shares one set of libraries. Project A needs version 2 of a library, project B needs version 3, and pip can only install one of them. The second install overwrites the first, and one project breaks. The same problem hits system tools: your operating system may rely on a Python package, and upgrading it for your project can break a system utility.

A virtual environment fixes this by giving each project its own package folder. Activating the environment points Python and pip at that folder. Deactivating it returns you to the global setup. Nothing about the project itself changes, only where its packages live. Deleting the environment folder removes every package installed for that project, which is also the cleanest way to start over when dependencies get tangled.

Keeping secrets and settings out of shared state matters here too. A project that stores its API keys in the environment folder or in a shared config will leak them across projects. The guide on how to store API keys and secrets safely shows how to keep credentials in per-project files that never get committed, which pairs naturally with one environment per project.

Python Virtual Environments venv: Creating Your First One

The venv module ships with Python 3.3 and later, so there is nothing to install. From your project folder, run:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip

The first command creates a folder called .venv with a fresh Python and pip. The second activates it: your prompt gains a (.venv) prefix, and python and pip now point inside the folder. The third upgrades pip inside the environment, which avoids old installer bugs. On Windows the activation command is .venvScriptsactivate instead.

Choose one folder name and stick with it. .venv is the common choice, and most editors detect it automatically. Add it to .gitignore so the environment never gets committed: it can be rebuilt from requirements.txt at any time, and it is often hundreds of megabytes. To leave an environment, run deactivate. To remove it entirely, delete the folder. There is no uninstall step and no registry to clean.

Installing Packages with pip Inside the Environment

With the environment active, pip installs into that environment only. The command is the same as always:

pip install requests
pip install "django>=4.0,<5.0"
pip list

Always use python -m pip instead of bare pip when something behaves oddly. The python -m form guarantees pip runs under the Python you think you are using, which matters when several Pythons share a machine. pip list shows what the environment holds, and pip show requests shows the version and location of one package.

Install order matters less than people think, but pinning does. A bare pip install requests grabs the newest version today and a different one next month, which is how a teammate’s fresh install behaves differently from yours. Pin versions for anything that ships, and keep a looser setup only for throwaway experiments. If a package fails to build, the error usually names a missing system library rather than a Python problem, and installing that library at the system level fixes it.

requirements.txt: Freezing and Rebuilding an Environment

requirements.txt is the file that records what an environment contains, so anyone can rebuild it. Generate it from a working environment with:

pip freeze > requirements.txt

That writes every installed package with its exact version, one per line. A teammate or a server rebuilds the same setup with:

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

pip freeze lists everything, including packages you installed only as dependencies of other packages. That output is exact but noisy. For a hand-written requirements file, many teams list only the packages they import directly, with version ranges, and let pip resolve the rest. Both approaches work. The frozen file is safer for production deploys, the hand-written one is easier to read and review. Pick one and use it consistently across a project.

Keep requirements files per environment, not per machine. A web app and its data scripts often need different packages, and sharing one file couples them. If a project grows a second environment, for example a test setup with extra tools, a second file such as requirements-dev.txt that starts with -r requirements.txt and adds the dev tools keeps the split explicit.

Daily Workflow and Common Mistakes

A solid daily routine looks like this: open the project, activate the environment, work, freeze requirements when you add a package, deactivate when done. Editors such as VS Code can activate automatically once you point them at the .venv interpreter, which removes the step most people forget.

Three mistakes cause most venv trouble. The first is installing a package with the environment inactive, which puts it in the global Python where the project cannot see it. If an import fails right after an install, check the prompt for the (.venv) prefix before anything else. The second is committing the .venv folder, which bloats the repository and breaks on other machines. The third is using sudo pip install, which writes into system Python and can break operating system tools. Inside a virtual environment you never need sudo for pip.

When a project grows into a real application, the same isolation idea extends to deployment. A Python web app that runs in a container gets the same guarantee at a larger scale: the app ships with its own environment. The guide on containerizing an app with Docker for beginners shows the container side of that idea, and the walkthrough on Docker Compose explained for multi-container apps shows how to run the app plus its database together locally, and the venv habits you build now transfer directly.

Alternatives Worth Knowing

venv is the standard tool, and it is enough for most projects. Two alternatives appear often enough to know by name. virtualenv is an older third-party tool that predates venv and still works on older Pythons. Conda manages Python versions as well as packages, and data science teams often prefer it because it installs non-Python libraries too. Poetry and uv manage dependencies and lock files with more structure than requirements.txt, and teams that want reproducible builds sometimes move to them.

None of these replace the core idea: isolate each project. Learn venv first, since it ships with Python and every other tool assumes you understand the concept. Once the habit is set, switching tools is a small step. The environment is a means to an end, and the end is a project that runs the same way on your machine, your teammate’s machine, and the server.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.