Skip to content

Pipenv

pipenv is for Python what NPM is for Node.js. However, pipenv is used for much more than package management, and it also creates and manages a virtual environment for you.

Environment Management with Pipenv

The three primary commands you’ll use in managing your pipenv environment are:

$ pipenv install,
$ pipenv uninstall, & 
$ pipenv lock.

In the previous chapters, we used virtualenv to create our virtual environment; however, Kenneth Reitz (the creator of the popular package requests) created pipenv. pipenv is for Python what NPM is for Node.js. However, pipenv is used for much more than package management, and it also creates and manages a virtual environment for you. In my opinion, there are a lot of advantages of the old development workflows, but for me, there are two things that stand out: the first is that you no longer need two different tools (pip, virtualenv), and the second is that it is much simpler to have all these great features in just one place.

Another thing that I really like about pipenv is the use of Pipfile. Sometimes, it is really hard to work with requirement files. Our production environment and development environment have the same dependencies, and you end up having to maintain two different files; plus, every time you need to remove one dependency, you will need to edit the requirement file manually.

With pipenv, you don't need to worry about having multiple requirement files. Development and production dependencies are placed in the same file, and pipenv also takes care of updating the Pipfile.

# Installing pipenv is quite simple, just run:
pip install pipenv

# After installing it you can run:
pipenv --help

# use pipenv to create our virtual environment
pipenv --python python3.6

pipenv install requests

The author of pipenv is the same developer who created the popular requests library.


$ pipenv install <package>

This will create a Pipfile if one doesn’t exist. If one does exist, it will automatically be edited with the new package your provided.

Next, activate the Pipenv shell:

$ pipenv shell
$ python --version

Specifying Versions of Python

To create a new virtualenv, using a specific version of Python you have installed (and on your PATH), use the --python VERSION flag, like so:

Use Python 3:

$ pipenv --python 3

Use Python3.6:

$ pipenv --python 3.6

Use Python 2.7.14:

$ pipenv --python 2.7.14

When given a Python version, like this, Pipenv will automatically scan your system for a Python that matches that given version.

If a Pipfile hasn’t been created yet, one will be created for you, that looks like this:

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

[dev-packages]

[packages]

[requires]
python_version = "3.6"

Reference