Working in a Python virtualenv
In this article we will discuss working in a virtual environment.
We covered installing virtualenv in the previous
article
We are in the directory virtualenvs inside which we created
a virtual environment for Python3 called test_env
To activate the virtual environment test_env
from the standard cmd in Windows execute
test_env\Scripts\activate.bat
If you are in PowerShell, execute
test_env\Scripts\activate.ps1
And if you are on Linux execute
. test_env/bin/activate
On successful activation the name of the virtual environment appears in parentheses on the left of the command prompt (test_env)
(test_env) PS C:\Users\Andrei\virtualenvs>
If nothing happened, read the advice in my article
«Difficulties when working with Python»
The first thing to do after entering a new environment is to check the Python and Pip versions
python -V
Python 3.8.2
pip -V
pip 20.1.1 from C:\Users\Andrei\virtualenvs\test_env\lib\site-packages\pip (python 3.8)
Let's check which modules are installed in the new virtual environment
python -m pip list
Package Version ---------- ------- pip 20.1.1 setuptools 46.4.0 wheel 0.34.2
There are only three modules, which means that something has indeed changed.
Compare this with the number of modules installed in the system from the lesson
pip
Let's install requests in the virtual environment
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's 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
requests 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 leave the virtual environment, use the command
deactivate
p class="p2"> If you run this nowpython -m pip show requests
span class="yellow">WARNING: Package(s) not found: requests
You can see that requests is not installed globally 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 don't know about you, but in my PowerShell (myvenv), created with venv, is highlighted in green, while test_env, created with virtualenv, stays grey.
Deleting the virtual environment
To delete the virtual environment, just delete the folder
Article author: Andrei Olegovich