venv Python
Introduction
The venv module supports the creation of lightweight "virtual environments", each of which has its own
independent set of Python packages installed in their site directories.
A virtual environment is created on top of an existing Python installation, known as the "base" Python of the virtual
environment, and can optionally be isolated from the packages in the base environment, so only those that are
explicitly installed in the virtual environment are available.
When used from a virtual environment, the usual installation tools such as pip will install Python packages
into the virtual environment without needing an explicit instruction to do so.
See
PEP 405
for more information about Python virtual environments.
Installation
It is part of Python3 so usually it does not need to be installed.
If you work in
WSL
such as
Debian
/
Ubuntu
it can be installed with
sudo apt install python3.10-venv
Create
To create a new environment, run
python3 -m venv venv
Instead of venv you can come up with your own name for the environment.
Usually for small projects nobody bothers and simply creates a venv
environment in the project folder - this approach has both pros and cons.
If you work in WSL then when creating the environment you need to use the same Python version for which you installed venv. For example
sudo python3.10 -m venv ubuntu_venv
Activate
On Linux to enter the environment, run
source ./venv/bin/activate
On Windows PowerShell you need to run
.\venv\Scripts\activate
An example PowerShell script that checks whether the environment from the current directory is activated and if not, activates it
If ($env:VIRTUAL_ENV -eq "${PWD}\venv") { Write-Host "venv is already activated" echo $env:VIRTUAL_ENV echo "${PWD}\venv" } Else { echo $env:VIRTUAL_ENV echo "${PWD}\venv" python -m venv venv .\venv\Scripts\activate }
Deactivate
To exit the virtual environment, run
deactivate
Choosing a Python version for the environment
To create an environment with a specific Python version use the command with the version specified.
For this to work you need to have Python of that version installed
python3.9 -m venv your_env
Save requirements
python -m pip freeze > requirements.txt
The file requirements.txt has the format
package_name==version
For example
flake8==7.0.0 mccabe==0.7.0 pycodestyle==2.11.1 pyflakes==3.2.0 robotframework==7.0 six==1.16.0 thrift==0.16.0
Install from requirements.txt
python -m pip install -r ./requirements.txt
On Windows
python -m pip install -r .\requirements.txt
requirements from GitHub
In the file requirements.txt you can use not only package names with their versions, but also links to GitHub repositories, if these repositories are packaged as packages.
git+https://github.com/AndreiOlegovich/demo_py_package.git
Or with a commit specified
git+https://github.com/AndreiOlegovich/demo_py_package.git@470a6844f71eb8872174d8fe28629337c3d69fab
Checking activation
The environment name may not appear in brackets in the terminal prompt.
To check whether the virtual environment has been activated or not, you can run the following commands
On Linux
python >>> import sys >>> sys.prefix '/home/andrei/pytest/venv' >>> sys.base_prefix '/home/andrei/.pyenv/versions/3.9.5'
On Windows
python >>> import sys >>> sys.prefix 'C:\Users\Andrei\Documents\Projects\venv' >>> sys.base_prefix 'C:\Users\Andrei\AppData\Local\Programs\Python\Python310'
If sys.prefix differs from sys.base_prefix then the Python from the virtual environment is being used.
If you run the script in Pycharm study the terminal output
/home/andrei/pytest/venv/bin/python /home/andrei/pytest/main.py
Process finished with exit code 0
/home/andrei/pytest/venv/bin/python
- this is the path to the Python used
If I had run the script not from the virtual environment, instead of this path there would be the path
/usr/bin/python3.8
Article author: Andrei Olegovich