Pipenv

Pipenv Intro

Pipenv is a great Python Dev workflow for humans. It was designed for replacing Pip, venv, and virtualenv. By using Pipenv, you have the easiest way to manage your Python environments, including package version control, minimum project size, dev & prod env isolation, migration, and teamwork.

Pipenv Installation

To install Pipenv:

1
pip install --user pipenv

Pipenv Manual Page:

1
pipenv --help

Activate Pipenv Virtualenv

To activate pipenv with a Python virtual environment (virtualenv):

1
pipenv shell

Pipenv will create a file named Pipfile to record your Python version and packages info if you don’t have this file. This is Pipenv’s requirements.txt. And once you install your first package, you will also have a Pipfile.lock, which includes the sha256 hashes of each downloaded package.

To exit pipenv shell:

Press control + D (Windows);
Press command + D (MacOS)


Package Management

To install packages (and dependencies) and add them to Pipfile:

1
2
3
4
5
6
7
8
pipenv install [package name]

# examples
pipenv install pandas
pipenv install "pandas==1.4" # will install a version equal or larger than 1.4.0
pipenv install "pandas<=1.4" # will install a version equal or lower than 1.4.0
pipenv install "pandas>1.4" # will install 1.4.1 but not 1.4.0
pipenv install "pandas~=1.4" # will install latest version (minor update) >=1.4 and before 2.0

To install packages to dev:

1
pipenv install [package name] --dev

To uninstall packages, and remove dependencies:

1
2
pipenv uninstall [package name]
pipenv clean

To review your installed packages with dependency graph information:

1
pipenv graph

To review your Pipfile and Pipfile.lock:

1
2
cat Pipfile
cat Pipfile.lock

Migration to or from Pip

To freeze a requirements.txt for Pip (useful when others only use pip):

1
pipenv lock -r > requirements.txt

To install packages from requirements.txt:

1
pipenv install -r requirements.txt

Pipenv in child directory

If the parent directory contains a Pipfile and you would like to activate another Pipenv in the child directory:

1
2
3
# In the child directory
touch Pipfile
pipenv shell

Pipenv’s Virtual Environment

Pipenv’s virtual environment is built on virtualenv.

To review the location of Pipenv’s virtualenv:

1
pipenv --venv

To remove your Pipenv’s virtualenv:

1
pipenv --rm

To set Pipenv’s virtualenv in the same directory of your project dir (this is a global option):

1
2
3
4
5
6
7
export PIPENV_VENV_IN_PROJECT=1

# to see your .venv
ls -la

# to deactivate the option
unset PIPENV_VENV_IN_PROJECT

To migrate your entire project with virtual env:

Enable PIPENV_VENV_IN_PROJECT and copy your project dir to the target machine. DON’T change the project dir name due to the rule of virtual env.

On the target machine, in the project dir:

1
pipenv shell

Your migrated project is ready to run with all required packages and dependencies (in .venv of your project dir) , no Internet connection needed.


References

Pipenv
Pipenv: Python Dev Workflow for Humans
Make Pipenv create the virtualenv in the same folder