Python Virtual Environments
How to create, use, and manage isolated Python environments for your projects
When you work on more than one Python project, you will quickly run into a problem: different projects need different versions of the same library. If you install everything globally, the projects start to conflict with each other. A virtual environment solves this by giving each project its own isolated set of packages, completely separate from everything else on your system.
What a Virtual Environment Is
A virtual environment is a self-contained directory that holds a Python interpreter and a set of installed packages. When the environment is active, any pip install command installs into that directory, not into the global Python installation. This means you can have different packages and different library versions in each project without them interfering with each other.
Creating a Virtual Environment
Navigate to your project folder and run:
python -m venv venvThis creates a folder called venv inside your project. The name venv is a convention, not a requirement. Some people use .venv to keep it hidden from directory listings.
Activating the Environment
You need to activate the environment before using it. The command differs by operating system.
Linux and macOS:
source venv/bin/activateWindows (Command Prompt):
venv\Scripts\activate.batWindows (PowerShell):
venv\Scripts\Activate.ps1Once active, your terminal prompt changes to show the environment name, like (venv). You can confirm it is working by running which python on Linux/macOS or where python on Windows. The path shown should point inside your venv folder.
Installing Packages
With the environment active, install packages normally:
pip install requests
pip install flaskThese packages go into the venv folder and are only available when this environment is active. They do not affect any other project.
Saving and Sharing Your Dependencies
To record exactly which packages are installed, run:
pip freeze > requirements.txtThis creates a requirements.txt file listing every installed package and its version. Commit this file to your repository. When someone else sets up the project, they run:
pip install -r requirements.txtThat installs all the same packages in their environment.
Deactivating the Environment
When you are done working, deactivate it:
deactivateYour prompt returns to normal. The environment still exists; you just need to activate it again next time.
Worked Example
Here is the full workflow for starting a new project:
# Create the project folder
mkdir my-project
cd my-project
# Create and activate the environment
python -m venv venv
source venv/bin/activate # Linux/macOS
# Install what you need
pip install flask
# Save the dependencies
pip freeze > requirements.txt
# Work on your project...
# When done
deactivateWhat to Add to .gitignore
The venv folder is large and machine-specific. Add it to your .gitignore file so it is not committed to version control:
venv/
.venv/Your requirements.txt file is what gets committed. It is small, readable, and all anyone needs to recreate the environment.
More setup guides
Not sure your computer can run a model? Use the will-it-run checker.

