Pipenv cheat sheet for npm users

Pipenv cheat sheet for npm users
npm initpipenv --python 3
npm installpipenv install
npm install lodash@4.17pipenv install pandas@1.1
npm install --save-dev lodash@4.17pipenv install --dev pandas@1.1
npm updatepipenv sync / pipenv update
npm update --devpipenv sync --dev
npm lspipenv graph
npm runpipenv run
# open a shell in virutal envpipenv shell
# end shell in virutal envexit
# save virtual env in current direxport PIPENV_VENV_IN_PROJECT=1

Why pipenv

I resisted using virtual environments in Python because they cause extra overhead and aren't project specific. On the other hand, I fell in love with npm at first sight, because it operates within the project directory and is aligned with actual developer objectives. Pipenv is a npm equivalent for Python, a way out of having to think about virtual environments.

Quick start

First install pipenv from pip, and it'll be the only thing to install globally into the OS.

pip install pipenv

Configure pipenv to keep virtual environment files in the project directory. (I recommend adding this line to your shell init).


Then create a new directory for our project and change into it.

mkdir pipenv-demo
cd pipenv-demo

Create a managed Python environment with desired version.

pipenv --python 3

Install pandas in the environment.

pipenv install pandas

Once finished, there should be a Pipfile in the directory.

url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

pandas = "*"


python_version = "3.6"

Pipefile and Pipfile.lock (not shown here) work together to specify the information to reproduce this Python environment. Both files should be checked into version control, and another developer can use the install command to reproduce the environment from these two files.

pipenv install

There are two ways to use the environment. First we can activate the underlying virtual environment with the shell command. Once activated, the python command will point to the executable in the environment and all of the installed libraries will be available.

$ pipenv shell
(pipenv-demo) $ which python
(pipenv-demo) $ python
>>> import pandas
>>> pandas.__version__

The second and more useful way is to have pipenv run our Python file with the run command.

$ echo 'import pandas; print(pandas.__version__)' > check.py
$ pipenv run python check.py

Official doc is found here https://docs.pipenv.org/ .

Other tricks

I highly recommend always setting the PIPENV_VENV_IN_PROJECT environment variable, making pipenv download files into .venv under current directory, emulating npm's behavior of node_modules.


To blow away the virtual environment for reasons (version conflict / out of disk) and start fresh, simply remove the .venv directory.

rm -r .venv

A .env file is also supported for specifying run-time environment variables.

Why not to use pipenv

  1. I wish pipenv has more project automation features like npm start, or the scripts section in packages.json. Last time I checked, most power users are using Makefile for automation. I don't hate make, but maybe something more idiomatic?
  2. I struggle to get pipenv configured correctly in IDEs (PyCharm, VSCode, etc), so I generally end up typing the pipenv commands instead of clicking the play button.
  3. conda is a popular alternative.