venv (virtual environments) in python
You can download the project of this blog post clicking here.
Virtual environments in python (venv) are a way that we can isolate different projects from each other. Sometimes When we work with python, each time we don’t have a dependency, what we do is:
sudo pip install DEPENDENCY; for example
sudo pip install sympy
This way, we have the library available globally, with all the consequences. If we have two projects that use that library, and one of them needs a new feature of a new version, if we have it globally, we update globally, with the consequent risk for the other application. A good practice is to control the versions of the software that you have installed, and before upgrading versions (especially if it is a major version update) do all the testing. With virtual environments we can have the application environment a with the version x of that library and an application b with a z version of that library. Usually globally we only want to have, setuptools, pip and the virtual environments themselves.
A good practice is for each project to have a requirements.txt file, this way when someone download a project, they can create a virtual environment, install all the dependencies with which the project is working, and start working on the project.
A requirements.txt file is something as easy as:
Using virtual environments
To install them, we type
sudo pip install virtualenv
Once it is installed, we can create a virtual environment with:
Using base prefix '/usr' New python executable in /home/dio/envexample/env3/bin/python3.5 Also creating executable in /home/dio/envexample/env3/bin/python Installing setuptools, pip, wheel...done.
After that, some files and folders have been created
ls -la total 32 drwxr-xr-x 5 dio dio 4096 jun 18 17:08 . drwxr-xr-x 3 dio dio 4096 jun 18 17:08 .. drwxr-xr-x 2 dio dio 4096 jun 18 17:08 bin drwxr-xr-x 2 dio dio 4096 jun 18 17:08 include drwxr-xr-x 3 dio dio 4096 jun 18 17:08 lib -rw-r--r-- 1 dio dio 60 jun 18 17:08 pip-selfcheck.json
The bin folder has scripts for interacting with the virtual environment and a copy of the python and pip interpreter among other libraries. The include folder contains support libraries if needed. The lib directory is where all of the python libraries installed on that virtual environment are.
Right now, we have a virtual environment, but we are not using it yet, we have to activate it first. To do this, we do (within the virtual environment folder):
cd env3 source bin/activate (env3) ➜ env3
The terminal prompt changes to tell us that we are using the virtual environment. Now, everything we use is within our virtual environment. If we list the packages we have installed:
pip list --format=columns Package Version ---------- ------- pip 9.0.1 setuptools 36.0.1 wheel 0.29.0
We see that we have a “basic” python installation in which we do not have any extra packages.
When we no longer need the virtual environment, or need to work with another, we have to deactivate it, if we don’t deactivate the virtual environment is going to be active during the session. To deactivate it we do:
And we see the the prompt as we had it before the activation of the virtual environment.
With the environment active, if we need to install a library (for example pytest), we do the same as we would do to install globally, but we do not need to be superusers:
(env3) ➜ env3 pip install pytest Collecting pytest Using cached pytest-3.1.2-py2.py3-none-any.whl Requirement already satisfied: setuptools in ./lib/python3.5/site-packages (from pytest) Collecting py>=1.4.33 (from pytest) Using cached py-1.4.34-py2.py3-none-any.whl Installing collected packages: py, pytest Successfully installed py-1.4.34 pytest-3.1.2 (env3) ➜ env3
If we download the tutorial project, and try to run the main.py, it will give us error since we do not have the necessary dependency. As we have said, if all dependencies of our project are in the requirements.txt file, it is as simple as run pip install -r requirements.txt to get all the dependencies.
(env3) ➜ python_venv_example git:(master) ✗ pip install -r requirements.txt Collecting sympy==1.0 (from -r requirements.txt (line 1)) Downloading sympy-1.0.tar.gz (4.3MB) 100% |████████████████████████████████| 4.3MB 434kB/s Collecting mpmath>=0.19 (from sympy==1.0->-r requirements.txt (line 1)) Downloading mpmath-0.19.tar.gz (498kB) 100% |████████████████████████████████| 501kB 2.9MB/s Building wheels for collected packages: sympy, mpmath Running setup.py bdist_wheel for sympy ... done Stored in directory: /home/dio/.cache/pip/wheels/05/93/22/2d0f59d842347b1f38df0d3f7a3870586df60568d2a49d94c5 Running setup.py bdist_wheel for mpmath ... done Stored in directory: /home/dio/.cache/pip/wheels/02/2b/99/cd867d5da48d951118a8020e86c0c12a65022702426582d4b8 Successfully built sympy mpmath Installing collected packages: mpmath, sympy Successfully installed mpmath-0.19 sympy-1.0 (env3) ➜ python_venv_example git:(master) ✗
If we check the dependencies we see that we have installed the ones that we had in the requirements.txt file
pip list --format=columns Package Version ---------- ------- mpmath 0.19 pip 9.0.1 py 1.4.34 pytest 3.1.2 setuptools 36.0.1 sympy 1.0 wheel 0.29.0