Setting up Pipenv

Migrate your Python project to Pipenv.

September 24, 2018 - 2 minute read -
python

Assumptions:

  • You have a Python project in a virtual environment.
  • Your project’s top-level directory contains your env/ folder, .env and requirements.txt files.

Install Pipenv as explained in the docs.

cd into your project’s root directory (you hopefully have your env/ folder, .env and requirements.txt files in the root directory) and optionally switch to a branch other than master:

foo@bar:~$ cd my_project
foo@bar:~$ git checkout -b migrate_to_pipenv

(Sidenote: Check out Oh My Zsh and wd for a supercharged terminal)

Spawn a shell. This will generate a Pipfile from your requirements.txt

foo@bar:~$ pipenv shell
requirements.txt found, instead of Pipfile! Converting…
Warning: Your Pipfile now contains pinned versions, if your requirements.txt did.
We recommend updating your Pipfile to specify the "*" version, instead.
Spawning environment shell (/bin/zsh). Use 'exit' to leave.

The generated Pipfile will probably have pinned versions. It’s recommended that you convert these to ‘*’ versions (anyone knows of an automated way to do this?).

certifi = "==2017.11.5"     # convert this to
certifi = "*"

Install the packages to the new environment (this will update your packages). This will also generate a Pipfile.lock:

foo@bar:~$ pipenv install
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (bd2078)!
Installing dependencies from Pipfile.lock (bd2078)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/5 — 00:00:01

Update your requirements.txt:

foo@bar:~$ pipenv lock -r > requirements.txt