How to Set Up a Python Virtual Environment Correctly

Python virtual environments isolate project dependencies from each other and from the system Python — essential practice for any serious Python deployment, avoiding dependency conflicts between projects.

Why Virtual Environments Matter

Without isolation, installing packages globally means every project shares the same dependency versions — if Project A needs version 1.0 of a library and Project B needs version 2.0, a global installation can't satisfy both simultaneously; virtual environments solve this by giving each project its own isolated dependency set.

Creating a Virtual Environment

python3 -m venv venv

Creates a venv directory containing an isolated Python environment specific to this project.

Activating the Virtual Environment

source venv/bin/activate

Your shell prompt typically changes to indicate the active virtual environment — any pip install commands now install into this isolated environment, not system-wide.

Installing Packages Within the Virtual Environment

pip install requests flask

Saving Dependencies to requirements.txt

pip freeze > requirements.txt

Captures exact installed versions, letting others (or your production server) recreate the identical environment.

Installing from requirements.txt

pip install -r requirements.txt

Deactivating the Virtual Environment

deactivate

Returns to your normal shell environment, no longer using the isolated Python/packages.

Never Commit the venv Directory to Version Control

echo "venv/" >> .gitignore

Virtual environments are environment-specific and can be large — commit only requirements.txt, letting anyone recreate the environment themselves rather than committing the actual environment directory.

Using Virtual Environments in Production Deployment

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

Your production server should also use a dedicated virtual environment, not install dependencies globally — keeps your deployment consistent with development and avoids any system-level Python conflicts.

Referencing the Virtual Environment in systemd Services

ExecStart=/opt/myapp/venv/bin/gunicorn myapp:app

Point directly at the virtual environment's specific binary path, ensuring the service uses the correct isolated Python/dependencies rather than accidentally using system Python.

Combining with pyenv for Version + Dependency Management

See How to Manage Multiple Python Versions with pyenv — use pyenv to select the correct Python version, then create a virtual environment using that specific version, giving you full control over both Python version and package isolation.

Alternative Tools: Poetry and pipenv

Beyond the built-in venv module, tools like Poetry and pipenv offer additional dependency management features (better dependency resolution, integrated lock files) — worth exploring for more complex projects, though venv alone is entirely sufficient for most straightforward deployments.

Common Errors

"command not found" for a package's CLI tool after installing in a venv — verify the virtual environment is actually activated (or you're referencing its binary path directly); the tool exists only within the isolated environment, not system-wide.

Continue Reading

Browse more articles in Programming Languages & Runtimes.

  • python virtual environment, python venv tutorial, requirements.txt, python dependency isolation
  • 0 Kunder som kunne bruge dette svar
Hjalp dette svar dig?

Relaterede artikler

How to Install PHP on Ubuntu & Debian (Complete Guide)

PHP powers a huge share of the web, including WordPress, Laravel, and Magento. This guide covers...

How to Install Node.js on Ubuntu & Debian (with NVM)

Node.js is a fast JavaScript runtime for building APIs, web applications, and backend services....

How to Install Python on Ubuntu & Debian

Python powers web frameworks like Django and Flask, automation scripts, and data applications....

How to Install Java (OpenJDK) on Ubuntu & Debian

Java powers enterprise applications, build tools like Maven and Gradle, and platforms like...

How to Install Composer for PHP Dependency Management

Composer is PHP's standard dependency manager, used by virtually every modern PHP framework...