Skip to content

Instantly share code, notes, and snippets.

@theskinnycoder
Created July 3, 2020 11:56
Show Gist options
  • Save theskinnycoder/c8f9aa7b5172f28d9f4268867ae60bfd to your computer and use it in GitHub Desktop.
Save theskinnycoder/c8f9aa7b5172f28d9f4268867ae60bfd to your computer and use it in GitHub Desktop.

Pipenv Crash Course

  • pipenv is a virtual environment creating tool(like venv), which ships with its own package manager(like pip).

  • It is similar to conda(Anaconda), cz it creates environment related files in a default location (for example, /home/<user_name>/.local/share/virtualenvs/ in Unix). But the latter is used in Data Science and ML.

  • It creates Pipfile(TOML) and Pipfile.lock(JSON) files. The former is like a replacement to requirements.txt. The latter is for consistent and deterministic builds, mostly for other developers to install the exact versions.

  • pipenv automatically identifies files like .env, requirements.txt, .flaskenv etc.

1) Install pipenv

A) Windows :

# Run this as Administrator
pip install pipenv

B) Unix :

sudo pip3 install pipenv

2) Activate or Create the virtual environemnt

This command creates and activates the virtual environment if doesn't already exists, and just enables/activates it if it already exits.

pipenv shell

3) Deactivate/Exit or Delete the virtual environment

  • This command exits or deactivates the virtual environment.
exit
# Or press Ctrl+D
  • This command deletes the virtual environment, but leaves the Pipfile and Pipfile.lock files.
pipenv --rm

4) Check and verify which python is being run

A) Using REPL

# 1) To enter into REPL
# a) Windows
python
# b) Unix
python3
# 2) Check
>>> import sys
>>> sys.executable
>>> quit()

B) Using Shell

a) Windows

:: Command Prompt
where python
# PowerShell
Get-Command python

b) Unix

which python

NOTES :

  • To check where the virtual environment files are located :
pipenv --venv
  • To check where the project home is located :
pipenv --where

5) Installing and Uninstalling Packages

1) Default Packages

# Install flask
pipenv install flask

# Uninstall flask
pipenv uninstall flask

2) Develop Packages

# Install as develop packages
pipenv install autopep8 flake8 --dev

# Uninstall the develop packages
pipenv uninstall autopep8 flake8 --dev

3) Others

A) Re-Install from Pipfile

This assumes you have the Pipfile.lock and Pipfile files.

# Install all default packages from Pipfile
pipenv install

# Install all default and develop packages from Pipfile
pipenv install --dev

NOTE : To re-install the virtual environment with a specific version of Python :

# Perform it after updating the python version in Pipfile as 3.6
pipenv install --python 3.6
# Or
pipenv --python 3.6

# Perform it if you want to install all packages with Python2
pipenv install --two

# Perform it if you want to install all packages with Python3
pipenv install --three

B) Uninstall all packages

# Uninstall all default packages
pipenv uninstall --all

# Uninstall all develop packages
pipenv uninstall --all-dev

6) Update packages

pipenv update runs both pipenv lock and pipenv sync

A) Update a specific package

pipenv update flask

B) Update all packages

pipenv update

NOTE :

What is pipenv sync?

  • It is similar to pipenv install --ignore-pipfile, but doesn't re-lock the dependencies.
  • It installs the exact versions as in Pipfile.lock.

7) Check dependency graph

Gives a tree-like structure of all the dependencies and thier dependencies

pipenv graph

8) From and To venv and pip

A) To pipenv

  • pipenv by default installs packages from a requirements.txt file, if it exists.
  • We can explicitly ask it to do so, too.
# Install packages from a requirements.txt file
pipenv install -r requirements.txt

# Install develop packages from a dev-requirements.txt file
pipenv install -r dev-requirements.txt --dev

B) From pipenv

# Create a requirements.txt for packages
pipenv lock -r > requirements.txt

# Create a dev-requirements.txt for develop packages
pipenv lock -r -d > dev-requirements.txt

9) Before Deployment

A) Set lockfile ready

  • This will take a snapshot of all the packages with exact versions
  • Similar to pip freeze
pipenv lock

B) Ignore pipfile

  • You don't want to install packages based on a Pipfile.
  • One should always depend on Pipfile.lock as it is precise and exact.
pipenv install --ignore-pipfile

10) Check security vulnerabilities

Use this command to see any vulnerable security issues. If exists, update the Pipfile manually.

pipenv check

11) Run with pipenv

pipenv run <any_command>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment