Pipenv

Table of contents
  1. Advantage to venv
  2. Basic usage
    1. Install
    2. Create environment and install
    3. Activate and deactivate environment
      1. Activate
      2. Deactivate
    4. Create Pipfile.lock
    5. Delete environment
  3. Use pipenv with a specific Python version

Advantage to venv

Pipenv’s Pipfile serves as an upgrade to requirements.txt.

It allows you to separately mark production and development dependencies.


Basic usage

Install

brew install pipenv

Create environment and install

Go to the desired project folder. To create and use a virtual environment for this root:

pipenv
pipenv install <package-name>  # Install specific package

If there’s already a Pipfile, create env and install listed requirements:

pipenv install  # With Pipfile

All the pipenv environments are in ~/.local/share/virtualenvs/root-dir-name-hash/ by default.

Activate and deactivate environment

Activate

pipenv shell

Deactivate

exit

Do not use deactivate.

Create Pipfile.lock

pipenv lock

Delete environment

To delete the environment for current directory:

pipenv --rm

Use pipenv with a specific Python version

You can set a specific version of Python when creating a pipenv virtual environment.

pipenv --python 3.x

However, it requires that Python 3.x is already installed on your local machine unlike conda create -n myenv python=3.x.

You can either,

  1. (Not recommended) brew install the desired Python 3.x
  2. (Recommended) Use pyenv
  3. (Meh..) Create a conda environment with the desired version and only use the binary

Then navigate to the root of the project and make Pipenv use the active Python:

pipenv --python=$(which python) --site-packages  # Creates an env in cwd
pipenv run which python  # It will point to a binary in `~/.local/share/virtualenvs/some-root-dir-hash/bin/python`
pipenv run python -V  # Check that it is indeed 3.x