DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 963,503 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Stefan Ukena
Stefan Ukena

Posted on • Originally published at blog.ukena.de on

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

Top comments (0)

๐ŸŒš Friends don't let friends browse without dark mode.

Sorry, it's true.