Using `pipenv` - The Basics


Just a couple of tiny recipes for pipenv

How to migrate an existing project from requirements.txt to a pipenv Pipfile

This will create a new virtual environment for your current folder and install all requirements from requirements.txt. Put another way: This will “convert” your requirements.txt into a Pipfile:

Run this from the project folder containing the requirements.txt file.

$ touch Pipfile
$ pipenv install -r requirements.txt

Output:

Creating a Pipfile for this project…
Requirements file provided! Importing into Pipfile…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success! 
Updated Pipfile.lock (037725)!
Installing dependencies from Pipfile.lock (037725)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 42/42 — 00:00:07
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

Next step: verify that the generated Pipfile contains all your requirements.

Feel free to delete the requirements.txt. You can always create a new one by calling pipenv run pip freeze > requirements.txt.

Caveats

  • If your requirements.txt contains -e . this will be converted to an absolute path in the generated Pipefile. You probably do not want this, because it will break on other machines with different paths. To fix: Open your Pipfile and change the absolute path from file = "file:///Users/..." simply to file = ".". (Source)

Note

touching the Pipfile is important, in case you already have a Pipfile in a parent-folder of your current path. This has tripped me up so many times.

How to create a virtual environment for a specific version of Python

Obviously, the specified version should be installed on your system first. Pipenv will not handle that for you.

To use any python 3 version:

$ touch Pipfile
$ pipenv --python 3

To use python version 3.8.1:

$ touch Pipfile
$ pipenv --python 3.8.1

See also