Managing Python Virtual Environments
How to manage Python virtual environments (venvs) for clean and isolated Python project setups.
How to manage Python virtual environments (venvs) for clean and isolated Python project setups.
Created 2025-10-23
Updated 2025-12-20
When working on Python projects, managing dependencies can quickly become messy. Virtual environments (venvs) let you isolate each project's packages, avoid version conflicts and keep your global Python installation clean. Using venvs also makes it easy to reproduce setups across different machines or share your project with others.
python3 -m venv myvenv
Activating a venv is like entering the virtual environment. Inside the venv you install packages with pip and run your Python scripts.
source myvenv/bin/activate
Deactivating means exiting your virtual environment.
deactivate
deactivate
rm -r myvenv
The requirements.txt is a special file used to store the project's dependecies. It's like the package.json file for the npm ecosystem and can be pushed to GitHub to share it with others.
After activating the venv, run:
pip3 install -r requirements.txt
You don't have to create the requirements.txt manually. Instead, you can use the freeze command to export the packages into a file.
Once again, make sure you activated the venv before running the command:
python3 -m pip freeze > requirements.txt