- Courses
- 💻Programming Languages
- 🐍Python
- Intermediate Python & Best Practices
- Virtual Environments
On this page
Virtual Environments
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
Why Virtual Environments Matter
A virtual environment is an isolated Python workspace that contains its own interpreter and its own installed packages.
This isolation prevents conflicts between projects and ensures that each project uses the exact dependencies it requires.
Virtual environments solve critical problems:
- Different projects need different package versions
- Global installations cause conflicts and break dependencies
- Reproducibility becomes impossible without isolation
- Deployment environments must match development environments
Professional Python development always uses virtual environments.
How Virtual Environments Work
A virtual environment is simply a directory containing:
- A dedicated Python interpreter
- A local
site-packagesfolder - Scripts for activating the environment
When activated, the environment overrides the system Python so that:
pythonrefers to the environment’s interpreterpipinstalls packages into the environment only- The project becomes self‑contained and reproducible
Creating a Virtual Environment
Use Python’s built‑in venv module.
python3 -m venv .venv
This creates a .venv directory inside your project.
.venv is a common, professional naming convention because:
- It is hidden by default in many tools
- It keeps the project root clean
- It avoids conflicts with other folders
Activating a Virtual Environment
Activation modifies your shell so that the environment’s Python is used.
macOS / Linux
source .venv/bin/activate
Windows (PowerShell)
.venv\Scripts\Activate.ps1
Once activated:
- Your shell prompt usually changes
pythonnow refers to.venv/bin/pythonpip installinstalls packages into.venvonly
Deactivating a Virtual Environment
To return to the system Python:
deactivate
This restores your original shell environment.
Installing Packages Inside the Environment
Once activated:
pip install requests
Packages are installed only inside .venv, not globally.
You can verify:
pip list
This shows only the environment’s packages.
Requirements Files for Reproducibility
A requirements file records the exact package versions used in a project.
Generate it:
pip freeze > requirements.txt
Install from it:
pip install -r requirements.txt
This ensures:
- Team members use identical dependencies
- Deployments match development
- Version conflicts are avoided
Using Virtual Environments in IDEs
Modern IDEs automatically detect .venv:
- VS Code
- PyCharm
- Sublime Text
- Vim/Neovim with Python plugins
You can also manually select the interpreter if needed.
Best Practices
- Create a
.venvfor every project - Add
.venvto.gitignore - Use
requirements.txtfor reproducibility - Use clear, consistent naming (
.venvorenv) - Never install packages globally unless absolutely necessary
- Rebuild environments cleanly when upgrading dependencies
These practices ensure clean, conflict‑free development environments.
Summary
Virtual environments provide isolated, reproducible Python workspaces that prevent dependency conflicts and ensure professional‑grade project organization.
You learned how to create, activate, deactivate, and manage virtual environments, how to install packages safely, and how to maintain reproducibility with requirements files.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Typing & Type Hints
Next
Packaging & Distribution
-
Why Virtual Environments Matter
-
How Virtual Environments Work
-
Creating a Virtual Environment
-
Activating a Virtual Environment
- macOS / Linux
- Windows (PowerShell)
-
Deactivating a Virtual Environment
-
Installing Packages Inside the Environment
-
Requirements Files for Reproducibility
-
Using Virtual Environments in IDEs
-
Best Practices
-
Summary
