Virtual Environments in Python with Pipenv

pipenv is a great tool to manage virtual environments in Python.

You can use pip to install it:

$ pip install pipenv

If you just want to start a brand new virtual environment without specifying any starting dependency, just run this command inside the folder:

$ pipenv install

Test Pipenv by creating a directory test_pipenv and installing the requests package.

Inside the test_pipenv folder run:

$ pipenv install requests==2.23.0

It might take a while to download and install the package for the first time.

Notice Pipenv creates two files Pipfile and Pipfile.lock.

If you look inside Pipfile, you will see something similar to this:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
requests = "==2.23.0"

[requires]
python_version = "3.8"

It is a file that describes the Python version required as "3.8" and which packages the project uses.

Since we installed requests in version 2.23.0, it is listed there.

The file Pipfile.lock is based on Pipfile and enforces the specific version of every package, protecting your project from breaking due to automatic upgrades.

Pipfile.lock is not very readable compared to Pipfile, you can look at it and see some similarities, don’t worry about it too much since it is automatically generated, Pipfile is the only file you have to actually manage, adding and removing packages.

This guarantees that when we share our project with someone, that person will have the same packages and versions installed in their virtual environment.

You can also install packages specifically for development, so they won’t be installed in production by using the --dev option.

For instance, pylint, as defined in the official project website, is a Python static code analysis tool which looks for programming errors, helps enforcing a coding standard, sniffs for code smells and offers simple refactoring suggestions.

So it is a package that only makes sense for development, not for production.

$ pipenv install --dev pylint

The Pipfile now looks like this:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
pylint = "*"

[packages]
requests = "==2.23.0"

[requires]
python_version = "3.8"

Notice the [dev-packages] section lists only pylint.

Activating the virtual environment

Notice the * in pylint, since we didn’t specify a version, it assumes the latest one available.

Now, to actually use the virtual environment we have to activate it, just run the command below:

$ pipenv shell

Your terminal should have the name of the folder we are into, test_pipenv in this case, surrounded by parentheses, similar to this:

(test_pipenv) bash-3.2$

To exit the virtual environment, just type exit:

(test_pipenv) bash-3.2$ exit

Uninstalling a package

If you installed a package and don’t want it anymore, just uninstall it using pipenv uninstall.

Here we are uninstalling requests:

$ pipenv uninstall requests

It will vanish from the Pipfile:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
pytest = "*"

[packages]

[requires]
python_version = "3.8"

Sharing a project

If someone else wants to work on your project, just share the project folder that already has a Pipfile in it, all they need to do inside the folder is to run:

$ pipenv install --dev

This command will automatically detect and read the Pipfile and install all the packages and other dependencies listed inside it.

The --dev tells pipenv to install both the development and the regular dependencies.

If you want to install only the dependencies under [packages], run:

$ pipenv install

This way you can be sure that your code is running as it should be with the right dependencies.