freeze Python
In this article we will discuss working with colleagues in a virtual environment.
You can study the installation of virtualenv in the article
«Installing virtualenv»
and working in it in the article
«Using virtualenv«
If in a command you see a \ in the path, it means it is for Windows
If it is /, then for Linux or MacOS
Often it is enough just to change the type of slash and the command will work on your OS
Suppose we are in the directory virtualenvs and have activated our
virtual environment myvenv
To check which package versions freeze sees, run
python -m pip freeze
or, if the command above showed nothing
python -m pip freeze --all
To write everything to a file, run
python -m pip freeze > requirements.txt
cat .\requirements.txt
certifi==2020.4.5.2
chardet==3.0.4
idna==2.9
requests==2.23.0
urllib3==1.25.9
In the file
requirements.txt
there is now a description of our virtual environment
myvenv, namely the list of all installed packages and their versions.
If you are familiar with
nmp
this file may remind you of
package.json
Now you can leave this environment, create a new one and activate it
deactivate
Let us create a new environment new_venv, activate it and see which packages are installed.
python -m venv new_venv
Then, if you are on Windows execute
nev_venv\Scripts\Activate.ps1
And if on Linux or MacOS, run
source ./nev_venv/bin/activate
After activation, run
python -m pip list
(new_venv) PS C:\Users\Andrei\virtualenvs> python -m pip list Package Version ---------- ------- pip 19.2.3 setuptools 41.2.0 WARNING: You are using pip version 19.2.3, however version 20.1.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
There are only two packages and even the pip version is lower than in the previous environment.
To install all the packages mentioned in the requirements.txt file, run
python -m pip install -r .\requirements.txt
Collecting certifi==2020.4.5.2 (from -r .\requirements.txt (line 1)) Using cached https://files.pythonhosted.org/packages/98/99/def511020aa8f663d4a2cfaa38467539e864799289ff354569e339e375b1/certifi-2020.4.5.2-py2.py3-none-any.whl Collecting chardet==3.0.4 (from -r .\requirements.txt (line 2)) Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl Collecting idna==2.9 (from -r .\requirements.txt (line 3)) Using cached https://files.pythonhosted.org/packages/89/e3/afebe61c546d18fb1709a61bee788254b40e736cff7271c7de5de2dc4128/idna-2.9-py2.py3-none-any.whl Collecting requests==2.23.0 (from -r .\requirements.txt (line 4)) Using cached https://files.pythonhosted.org/packages/1a/70/1935c770cb3be6e3a8b78ced23d7e0f3b187f5cbfab4749523ed65d7c9b1/requests-2.23.0-py2.py3-none-any.whl Collecting urllib3==1.25.9 (from -r .\requirements.txt (line 5)) Using cached https://files.pythonhosted.org/packages/e1/e5/df302e8017440f111c11cc41a6b432838672f5a70aa29227bf58149dc72f/urllib3-1.25.9-py2.py3-none-any.whl Installing collected packages: certifi, chardet, idna, urllib3, requests Successfully installed certifi-2020.4.5.2 chardet-3.0.4 idna-2.9 requests-2.23.0 urllib3-1.25.9 WARNING: You are using pip version 19.2.3, however version 20.1.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
And check what was installed
python -m pip list
Package Version ---------- ---------- certifi 2020.4.5.2 chardet 3.0.4 idna 2.9 pip 19.2.3 requests 2.23.0 setuptools 41.2.0 urllib3 1.25.9 WARNING: You are using pip version 19.2.3, however version 20.1.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
All packages are in place and in the required versions. This is true for not too complex projects. We will talk about possible problems at the end of the article.
Note that the pip version did not change.
python -m pip install requests
Collecting requests Downloading requests-2.23.0-py2.py3-none-any.whl (58 kB) |████████████████████████████████| 58 kB 609 kB/s Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 Downloading urllib3-1.25.9-py2.py3-none-any.whl (126 kB) |████████████████████████████████| 126 kB 2.2 MB/s Collecting chardet<4,>=3.0.2 Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB) |████████████████████████████████| 133 kB 2.2 MB/s Collecting certifi>=2017.4.17 Downloading certifi-2020.4.5.2-py2.py3-none-any.whl (157 kB) |████████████████████████████████| 157 kB 3.2 MB/s Collecting idna<3,>=2.5 Downloading idna-2.9-py2.py3-none-any.whl (58 kB) |████████████████████████████████| 58 kB 1.6 MB/s Installing collected packages: urllib3, chardet, certifi, idna, requests Successfully installed certifi-2020.4.5.2 chardet-3.0.4 idna-2.9 requests-2.23.0 urllib3-1.25.9
Let us check which packages were installed
python -m pip list
Package Version ---------- ---------- certifi 2020.4.5.2 chardet 3.0.4 idna 2.9 pip 20.1.1 requests 2.23.0 setuptools 46.4.0 urllib3 1.25.9 wheel 0.34.2
request pulled in four more packages: certifi, chardet, idna, urllib3
You can check where requests was installed in this environment with the command
python -m pip show requests
Name: requests Version: 2.23.0 Summary: Python HTTP for Humans. Home-page: https://requests.readthedocs.io Author: Kenneth Reitz Author-email: me@kennethreitz.org License: Apache 2.0 Location: c:\users\andrei\virtualenvs\test_env\lib\site-packages Requires: chardet, certifi, urllib3, idna Required-by:
To exit the virtual environment we use the command
deactivate
If you now run
python -m pip show requests
WARNING: Package(s) not found: requests
You can see that globally requests is not installed at all
Starting with Python 3.6 it is recommended to create virtual environments with the command
python -m venv myvenv
The environment is activated in the same way
myvenv\Scripts\Activate.ps1
(myvenv) PS C:\Users\Andrei\virtualenvs>
I do not know about yours, but on mine in PowerShell (myvenv), created with venv is highlighted in green, while test_env, created with virtualenv stays grey.
requirements.txt
Into the file
requirements.txt
the list of dependencies is exported.
By dependencies we mean the packages that are necessary for the project to work
Dependencies may have their own dependencies and there may be conflicts between them.
For example, your project depends on the packages X and Y. X depends on Z version 1.0 and
Y on Z version 2.0
and does not work with Z 1.0. But X in turn does not work with Z 2.0
What will happen in this case? I will not venture to say, but pip will install some version.
Article author: Andrei Olegovich