Skip to content

Instantly share code, notes, and snippets.

@samwelkanda
Last active August 8, 2019 15:57
Show Gist options
  • Save samwelkanda/5ec8469d44196867031ed834e01f89b2 to your computer and use it in GitHub Desktop.
Save samwelkanda/5ec8469d44196867031ed834e01f89b2 to your computer and use it in GitHub Desktop.

A GUIDE TO USING PIPENV FOR PYTHON PROJECTS

Spawn a shell in a virtual environment to isolate the development of an app:

$ pipenv shell

Force the creation of a Python 2 or 3 environment with the arguments --two and --three respectively.

Install the 3rd party package you need e.g

$ pipenv install django==2.2

Creates, a Pipfile and Pipfile.lock.

Install dependencies only for development with the --dev argument:

$ pipenv install pytest --dev

Providing the --dev argument will put the dependency in a special [dev-packages] location in the Pipfile.

So let’s say you’ve got everything working in your local development environment and you’re ready to push it to production. To do that, you need to lock your environment so you can ensure you have the same one in production:

$ pipenv lock

This will create/update your Pipfile.lock, which you’ll never need to edit manually. You should always use the generated file.

Now, once you get your code and Pipfile.lock in your production environment, you should install the last successful environment recorded:

$ pipenv install --ignore-pipfile

This tells Pipenv to ignore the Pipfile for installation and use what’s in the Pipfile.lock. Given this Pipfile.lock, Pipenv will create the exact same environment you had when you ran pipenv lock, sub-dependencies and all.

Now let’s say another developer wants to make some additions to your code. In this situation, they would use this command:

$ pipenv install --dev

This installs all the dependencies needed for development, which includes both the regular dependencies and those you specified with the --dev argument during install.

You can also show a dependency graph to understand your top-level dependencies and their sub-dependencies:

$ pipenv graph

Open a third-party package in your default editor with the following command:

$ pipenv open flask

You can run a command in the virtual environment without launching a shell:

$ pipenv run <insert command here>

Check for security vulnerabilities (and PEP 508 requirements) in your environment:

$ pipenv check

Now, let’s say you no longer need a package. You can uninstall it:

$ pipenv uninstall numpy

Additionally, let’s say you want to completely wipe all the installed packages from your virtual environment:

$ pipenv uninstall --all

You can replace --all with --all-dev to just remove dev packages.

Pipenv supports the automatic loading of environmental variables when a .env file exists in the top-level directory. That way, when you pipenv shell to open the virtual environment, it loads your environmental variables from the file.

How to find out where your virtual environment is:

$ pipenv --venv

How to find out where your project home is:

$ pipenv --where

To exit the current subshell

exit

or

ctrl+d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment