Python 3home |
Advanced Python
davidbpython.com
A virtual environment is an isolated Python setup for a single project.
A virtual environment gives each project its own independent Python setup. Each project gets:
This prevents different projects from interfering with each other.
Virtual environments prevent dependency conflicts between projects.
Different projects often require different versions of the same package. For example, one project may depend on an older version, while another requires a newer one.
Without virtual environments
pip install requests==2.25
pip install requests==2.31 # overwrites previous version
Installing a new version replaces the old one, which can break existing code.
Virtual environments isolate dependencies so each project can use its own versions.
Each project installs its own packages in its own environment.
Project A (in myprojects/population_analysis)
python -m venv venv
source venv/bin/activate
pip install requests==2.25
Project B (in myprojects/oceanic_temperatures)
python -m venv venv
source venv/bin/activate
pip install requests==2.31
Each project works independently without conflicts.
You create a virtual environment using the built-in venv module.
Python includes a module called `venv` for creating environments.
Basic command
cd myprojects/oceanic_temperatures
python -m venv venv
This creates a folder named "venv" in your project directory. (The folder should be called venv by convention, but can be named anything.) The venv folder contains everything needed for the environment.
The venv folder contains a full Python environment with its own tools and packages.
The virtual environment folder includes:
Typical structure:
Example
venv/
bin/ or Scripts/
lib/
You generally do not modify these files manually.
Activation switches your shell to use the virtual environment’s Python.
Before using the environment, you must activate it.
Command
source venv/bin/activate
After activation, your shell prompt typically changes: (venv) $ This indicates that the environment is active.
On Windows PowerShell, activation uses a different script.
The activation command differs slightly on Windows.
Command
.\venv\Scripts\Activate.ps1
If you see a security error, it is usually due to execution policy settings.
On Windows Command Prompt, activation uses a batch file.
If using the older Command Prompt (cmd):
Command
venv\Scripts\activate.bat
This achieves the same effect as PowerShell activation.
Activation redirects python and pip to the virtual environment.
When the environment is active, commands are redirected:
Check which Python is being used
where python # Windows
which python # macOS/Linux
The path should point inside the venv folder.
Packages installed after activation are placed inside the virtual environment.
Once activated, any installed package goes into the environment.
Example
pip install requests
This installation does not affect the global Python environment.
You can confirm installed packages using pip list.
To see what is installed in the current environment:
Command
pip list
Only packages installed in the active environment will appear.
Deactivation returns your shell to the system Python environment.
When you are finished working in the environment:
Command
deactivate
Your prompt will return to normal, and Python commands will use the global installation again.
Packages installed in a virtual environment are not visible outside it.
Install a package inside the environment:
Inside venv
pip install numpy
pip list
Then deactivate and check again:
Outside venv
pip list
The package will not appear outside the environment.
You can save and recreate environments using a requirements file.
To capture all installed dependencies:
Export dependencies
pip freeze > requirements.txt
To recreate the environment later:
Install dependencies
pip install -r requirements.txt
The install command will install all modules listed in requirements.txt. This ensures consistent environments across machines.
Most work with virtual environments follows a simple repeated sequence.
A typical workflow looks like this:
Standard flow
python -m venv venv
source venv/bin/activate
pip install flask
python app.py
deactivate
You repeat this process for each new project.
Most errors occur when the environment is not properly activated or configured.
Common issues include:
A frequent symptom:
Error example
ModuleNotFoundError
You can control which Python version a virtual environment uses.
If multiple Python versions are installed, specify one explicitly:
Example
python3.11 -m venv venv
This ensures the environment uses the intended interpreter.
The virtual environment folder should not be included in version control (i.e., with github).
The venv folder can be large and is machine-specific. Add it to `.gitignore`:
Example
venv/
Other users can recreate it using requirements.txt. We will discuss and use github in Session 4.
A virtual environment is a local, temporary Python setup tied to a project.
Key ideas to retain:
The essential commands are few and repeat across all projects.
Core commands:
Minimal set
python -m venv venv
source venv/bin/activate
pip install <pkg>
deactivate