Virtual Python Environment or venv is a Python environment which will help you to install different versions of Python modules in a local directory using which you can develop and test your code without requiring to install everything systemwide.


In Python3 we can use the venv module to create virtual environments.


We will create a directory call virtual inside which we will have two different 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

The first part of the prompt is now the name of the virtual environment, it will help you identify which environment you are in when you have multiple environments.

To deactivate the environment use deactivate command.

(virt1)$ deactivate

So, now we will install a Python module called redis.

(virt1)$ python3 -m pip install redis
Collecting redis
  Downloading redis-2.10.5-py2.py3-none-any.whl (60kB)
    100% |████████████████████████████████| 61kB 607kB/s
Installing collected packages: redis
Successfully installed redis-2.10.5

See also

Read this blog post from Brett Cannon to understand why you should use python3 -m pip to install packages.

Now we will create another virtual environment virt2 where we will install the same redis module but an old 2.4 version of it.

$ python3 -m venv virt2
$ source virt2/bin/activate
(virt2)$ python3 -m pip install redis==2.4
Downloading/unpacking redis
Downloading redis-2.4.0.tar.gz
Running egg_info for package redis
Installing collected packages: redis
Running install for redis
Successfully installed redis
Cleaning up...

This way you can have many different environments for all of your development needs.


Always remember to create virtualenvs while developing new applications. This will help you keep the system modules clean.


Pipenv is a tool created by Kenneth Reitz which helps to create, manage the virtualenvs for your projects. It also helps to install/uninstall/update the dependencies 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 pipenv install to create a new virtualenv for you. You can also pass any module name which pipenv will install on the environment.

$ mkdir myproject
$ cd myproject
$ pipenv install requests
Creating a virtualenv for this project…
Using /usr/bin/python3 (3.6.5) to create virtualenv…
⠋Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4l/bin/python3
Also creating executable in /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4l/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4l
Creating a Pipfile for this project…
Installing requests…
Collecting requests
Downloading (88kB)
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Downloading (133kB)
Collecting idna<2.7,>=2.5 (from requests)
Downloading (56kB)
Collecting certifi>=2017.4.17 (from requests)
Using cached
Collecting urllib3<1.23,>=1.21.1 (from requests)
Downloading (132kB)
Installing collected packages: chardet, idna, certifi, urllib3, requests
Successfully installed certifi-2018.4.16 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22

Adding 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:02
To activate this project's virtualenv, run the following:
$ pipenv shell

The above command will create a new virtualenv and then also install requests module in the environment. You can then use pipenv shell command to activate that environment. For our example, we will use the following Python code in a file named

import requests
response = requests.get('')
print('Your IP is {0}'.format(response.json()['origin']))
$ pipenv shell
$ $ python
Your IP is

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 will find two new files inside, Pipfile and Pipfile.lock. These files have been created by the pipenv command. You should checkin these two files into your version control system (say: git), so that others can create the exact same environment of yours.


The following is the content of our Pipfile. It is using the TOML file format.

verify_ssl = true
name = "pypi"
url = ""


python_version = "3.6.5"

requests = "*"

On the top it tells which source to use to get the packages. It also mentions the Python version required. The packages section tells us what all Python packages we need. The string “*” means install the latest version available on the package index. The exact version details of the packages are stored in the 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 you have any dependency which is only required for the development, you can install them marked as dev-packages. In the following example I am installing flake8 as development dependency.

$ pipenv install --dev flake8
$ cat Pipfile
verify_ssl = true
name = "pypi"
url = ""

"flake8" = "*"

python_version = "3.6.5"

requests = "*"

You can watch this talk by Kenneth from PyCon 2018 to know more about Pipenv.

Through out the rest of the book, we will use pipenv to create and manage virtualenvs for any code.