Virtual Python Environment or venv is a Python environment which will help youto install different versions of Python modules in a local directory using whichyou can develop and test your code without requiring to install everythingsystemwide.
Installation¶
In Python3 we can use the venv module to create virtual environments.
Usage¶
We will create a directory call virtual inside which we will have twodifferent virtual environment.
The following commands will create an env called virt1.
$ cd virtual$ python3 -m venv virt1$
Now we can activate the virt1 environment.
$ source virt1/bin/activate(virt1)[user@host]$
The first part of the prompt is now the name of the virtual environment, itwill help you identify which environment you are in when you have multipleenvironments.
To deactivate the environment use deactivate command.
(virt1)$ deactivate$
So, now we will install a Python module called redis.
(virt1)$ python3 -m pip install redisCollecting redis Downloading redis-2.10.5-py2.py3-none-any.whl (60kB) 100% |████████████████████████████████| 61kB 607kB/sInstalling collected packages: redisSuccessfully installed redis-2.10.5
See also
Read this blog post from Brett Cannon to understand why you should usepython3 -m pip to install packages.
Now we will create another virtual environment virt2 where we willinstall the same redis module but an old 2.4 version of it.
$ python3 -m venv virt2$ source virt2/bin/activate(virt2)$(virt2)$ python3 -m pip install redis==2.4Downloading/unpacking redisDownloading redis-2.4.0.tar.gzRunning setup.py egg_info for package redisInstalling collected packages: redisRunning setup.py install for redisSuccessfully installed redisCleaning up...
This way you can have many different environments for all of your developmentneeds.
Note
Always remember to create virtualenvs while developing new applications. This will help you keep the system modules clean.
Pipenv¶
Pipenv is a tool created by Kenneth Reitz which helps to create, manage thevirtualenvs for your projects. It also helps to install/uninstall/update thedependencies of your project.
Installing pipenv¶
We can install pipenv by the following command.
$ python3 -m pip install --user pipenv
Using pipenv¶
You can go to your project directory, and then use the command pipenvinstall to create a new virtualenv for you. You can also pass any modulename which pipenv will install on the environment.
$ mkdir myproject$ cd myproject$ pipenv install requestsCreating a virtualenv for this project…Using /usr/bin/python3 (3.6.5) to create virtualenv…⠋Already using interpreter /usr/bin/python3Using base prefix '/usr'New python executable in /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4l/bin/python3Also creating executable in /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4l/bin/pythonInstalling setuptools, pip, wheel...done.Virtualenv location: /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4lCreating a Pipfile for this project…Installing requests…Collecting requestsDownloading https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)Collecting chardet<3.1.0,>=3.0.2 (from requests)Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)Collecting idna<2.7,>=2.5 (from requests)Downloading https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)Collecting certifi>=2017.4.17 (from requests)Using cached https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whlCollecting urllib3<1.23,>=1.21.1 (from requests)Downloading https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)Installing collected packages: chardet, idna, certifi, urllib3, requestsSuccessfully installed certifi-2018.4.16 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22Adding requests to Pipfile's [packages]…Pipfile.lock not found, creating…Locking [dev-packages] dependencies…Locking [packages] dependencies…Updated Pipfile.lock (b14837)!Installing dependencies from Pipfile.lock (b14837)…🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/5 — 00:00:02To activate this project's virtualenv, run the following:$ pipenv shell
The above command will create a new virtualenv and then also installrequests module in the environment. You can then use pipenv shellcommand to activate that environment. For our example, we will usethe following Python code in a file named main.py.
import requestsresponse = requests.get('https://httpbin.org/ip')print('Your IP is {0}'.format(response.json()['origin']))
$ pipenv shell$ $ python main.pyYour IP is 192.168.1.2
Exiting from the virtualenv¶
You can exit from the virtualenv using exit command, or by pressing Ctrl+d.
Pipfile and Pipfile.lock¶
If you notice your project directory after you have used pipenv, you willfind two new files inside, Pipfile and Pipfile.lock. These files have beencreated by the pipenv command. You should checkin these two files intoyour version control system (say: git), so that others can create the exactsame environment of yours.
Pipfile¶
The following is the content of our Pipfile. It is using the TOML file format.
[[source]]verify_ssl = truename = "pypi"url = "https://pypi.python.org/simple"[dev-packages][requires]python_version = "3.6.5"[packages]requests = "*"
On the top it tells which source to use to get the packages. It also mentionsthe Python version required. The packages section tells us what all Pythonpackages we need. The string “*” means install the latest version availableon the package index. The exact version details of the packages are stored inthe Pipfile.lock file, it is in machine readable JSON format.
Remember to install any dependency for your project using pipenv comamnd,that will automatically update your Pipfile and Pipfile.lock file. If youhave any dependency which is only required for the development, you caninstall them marked as dev-packages. In the following example I am installingflake8 as development dependency.
$ pipenv install --dev flake8$ cat Pipfile[[source]]verify_ssl = truename = "pypi"url = "https://pypi.python.org/simple"[dev-packages]"flake8" = "*"[requires]python_version = "3.6.5"[packages]requests = "*"
You can watch this talk byKenneth from PyCon 2018 to know more about Pipenv.
Through out the rest of the book, we will use pipenv to create and managevirtualenvs for any code.