Python Virtual Environments Increase Quality
Photo by Adam Al Hafizh on Unsplash
The purpose of a virtual environment in Python is to control what modules and versions of modules will be used instead of the global collection of Python modules. You will have developed and tested with a specific set of Python modules. When you release a Python product to either an internal or external customer it may not work unless you control the environment. As sometimes happens, a newer module or earlier version of a module may cause your Project to fail. It’s best to provide the customer with a specific list of Python modules and versions.
This article is about the Python 3 module called ‘venv’, the Python 3 version of creating a virtual environment. For venv, you will need Python 3.3 +. ‘venv’ ships with Python 3.3+ so you have nothing to add.
Create a new virtual environment like this
$ python -m venv my_project
$ cd my_project/
$ ls
bin include lib lib64 pyvenv.cfg share
$ ls bin
activate activate.csh activate.fish Activate.ps1 easy_install easy_install-3.8 pip pip3 pip3.8 python python3
$
Notice, you just created many new files including copies of Pip and Python. The versions of Python/Pip are the same as the current environment.
Activate the Virtual Environment.
$ source project_env/bin/activate
$ python -V
Python 3.8.5
Notice the Python binary now resides inside the Virtual Environment.
$ which python
~/project_env/bin/python
Just a few default modules are installed in this Virtual Environment
$ pip list
Package Version
————- ——-
pip 20.0.2
pkg-resources 0.0.0
setuptools 44.0.0
$
Try installing the “requests” module in the virtual environment
(project_env)Computer:~$ pip install requests
Collecting requests
Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
|████████████████████████████████| 61 kB 2.8 MB/s
Collecting urllib3<1.27,>=1.21.1
…
The module list is longer. The module “requests” brings in supporting modules.
(project_env) allen@allenComputer:~$ pip list
Package Version
————- ———
certifi 2020.12.5
chardet 4.0.0
idna 2.10
pip 20.0.2
pkg-resources 0.0.0
requests 2.25.1
setuptools 44.0.0
urllib3 1.26.4
$
You can save the modules list for later in a text file.
pip freeze > requirements.txt
(project_env) ~/project_env$ cat requirements.txt
certifi==2020.12.5
chardet==4.0.0
idna==2.10
requests==2.25.1
urllib3==1.26.4
$
Closing Access to the Environment
$ deactivate
You may remove the Virtual Environment Directory if not needed.
You should be able to remove the Virtual Environments. For example, you may have a structure like my_project with your sources. A directory called my_project/venv may exist so it can be reinitialized as needed without affecting the source code.
By convention, use a Python Virtual Environment directory structure as below. Do not put source files in the venv directory since the venv directory might be deleted as requirements change. Also /venv would never be committed to source control since it can easily be rebuild with a requirement.txt file. Do source control the requirments.txt file.
The activate command gets you into the Virtual Environment
$ cd my_project
python3 -m venv my_projects/venv
source my_project/venv/bin/activate
If you have an old requirements.txt file you may want to run
pip install -r requirements.txt
To duplicate the original module list.
You can built your Virtual Environment and duplicate your global modules in your private environment with the system-site-package argument.
$ python3 -m venv venv –system-site-packages
$ source venv/bin/activate
You can see your local set of modules with the –local option. Local Modules are locally installed as needed.
$ pip list –local
The virtual environment is session based and is available no matter what directory you are in. The parenthesis indicate that you are using a virtual Python Environment. Notice, the parentheses goes away when you leave the Virtual Environment.
$ (project_env) ~/project_env$
$ deactivate
$ ~/project_env$
The commands as described above will be enough to setup and work in a Virtual Environment but there are more features with Virtual Environment you may want/need to explore for your project.
Other Solutions for a Virtual Environment
Python 2 has another similar module called virtualenv which has been replaced by the Python 3 version “venv”
https://virtualenv.pypa.io/en/stable/
pipenv is a Python module which does about the same thing as venv
See https://pypi.org/project/pipenv/
$ pip install pipenv
$ pipenv shell
Creating a virtualenv for this project…
⠹ Creating virtual environment…created virtual environment CPython3.8.5.final.0-64 in 146ms
creator CPython3Posix(dest…
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
Successfully created virtual environment!
Virtualenv location: /.local/share/virtualenvs/project_env-yGaAPzEe
requirements.txt found, instead of Pipfile! Converting…
Success!
…
$
These two solutions are less popular tools for virtual environments.
Summary:
Matching the right versions of supporting software packages has always been a challenge when the time comes to release a software product. Not having to rely on global environments will make any Project more robust. Many calls to support have been resolved when the customer’s environment was discovered to have a different version of software in the general release of the operating system. Having a requirments.txt file with all the right modules and version seems like the way to go.