pipenv#

Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world.

It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.

Installation#

pip install pipenv

Note

Generally, keep both Pipfile and Pipfile.lock in version control.

Pipfile#

Pipfile s contain information for the dependencies of the project, and supersedes the requirements.txt file used in most Python projects.

Import from requirements.txt#

If there is no Pipfile the install command will search for a requirement.txt and creates a Pipfile with its content.

pipenv install

Pipfile.lock#

This file lock all dependencies of the current installed packages

pipenv lock

Packages#

Install#

* meant the latest version on that level

# Install from requirements.txt
$ pipenv install

# Install one package
$ pipenv install <packagename>
$ pipenv install <packagename>=='<version>'
$ pipenv install Flask=='0.11.*'

# Install dev packages
$ pipenv install --dev <packagename>

Uninstall#

# uninstall one package
$ pipenv uninstall <packagename>

# uninstall all packages
$ pipenv uninstall --all

# uninstall all dev packages
$ pipenv uninstall --all-dev

Update#

Update all installed packages to the latest pip version

pipenv update

Run#

pipenv shell#

Each virtualenvironemnt has its own shell. In order to access it you have to launch it.

# Goto project folder with Pipfile
cd myproject

# Launch virtual environment
pipenv shell

# Exit virtual environment
exit

Run command in pipenv#

If you don’t want to use the virtual environment shell you can also just launch one command with:

pipenv run <commandname>
pipenv run python app.py

Virtual environment#

Create new pipenv#

In order to create a new virtual environment define the python version.

pipenv --python 3.6

Creating a new virtual environment with a specific python executable :

pipenv --python <path\to\python.exe\>

Remove pipenv#

If you want to remove a virtual envrironment launch

pipenv --rm

Dependency Graph#

pipenv graph

Workflow#

# Goto git repo
cd myproject

# Install environment if there is a Pipfile
# Also imports from requirements.txt if there is one
pipenv install

# Add new package to the project
pipenv install <package>

# Activate Pipenv shell
pipenv shell
python --version

# Lock all dependencies in file Pipenv.lock
pipenv lock

# Run environment for project
pipenv run <command>

Running jupyter notebook with pipenv#

After creating the pipenv virtual env, make sure ipykernel is installed as well:

pipenv install ipykernel

Activate the project’s virtual environment:

pipenv shell

Then connect the environment to a kernel:

python -m ipykernel install --user --display-name {YOUR_VIRTUAL_ENV_NAME} --name {YOUR_VIRTUAL_ENV_NAME}

Now, start your jupyter notebook/lab:

jupyter lab

Finally, in jupyter lab, select your new kernel by clicking on Kernel -> change kernel. Your newly created kernel should appear in the drop down menu.

If you want to list all environment kernel of your jupyter:

jupyter kernelspec list

Removing environment from jupyter kernel#

If you want to remove the environment kernel:

jupyter kernelspec uninstall {YOUR_VIRTUAL_ENV_NAME}

Python Pipenv