How to use pipenv in your python project

How to use pipenv in your python project

“goodbye requirements.txt, hello Pipfile”

In this article, I will introduce you to pipenv and show you how easy it is to use. pipenv removes the awkwardness of working with virtual environments and module dependencies. Once you spend 15 minutes with pipenv, you will realize the clarity and time savings it will bring to your python project.

Read Python for Data Analysis from O’REILLY on Safari with a 10-day trial.

What is pipenv?

pipenv was created by Kenneth Writze, the author of the extremely popular Requests package for python.  In his own words…

Pipenv is a dependency manager for Python projects. If you’re familiar with Node.js’ npm or Ruby’s bundler, it is similar in spirit to those tools. While pip can install Python packages, Pipenv is recommended as it’s a higher-level tool that simplifies dependency management for common use cases. – The Hitchhiker’s Guide to Python

The Python Software Foundation says pipenv and Pipfile are the recommended (if not Official) dependency management tools for python.

How pipenv works

  • pipenv keeps all virtual environment files in ~/.virtualenvs
  • pipenv keeps track of module dependencies in Pipfile (stored in the root of your project)
  • pipenv’s Pipfile replaces requirements.txt
  • pipenv keeps track of what’s been installed in the file Pipfile.lock

 

Here is an example Pipfile from one of my projects

How to install pipenv

pip install pipenv

Note: Depending on how python3 is installed you may need to type pip3 install pipenv

How to use pipenv

Create a new virtual environment

pipenv --python 3.6


Activate a virtual environment

pipenv shell


Executing code without activating your virtual environment

pipenv run python app.py

No need to activate your virtual environment each time, just type pipenv run followed by the command you would normally run after activating your venv.


Install a Package

pipenv install requests

pipenv install pandas

Traditionally, you would have to activate your virtualenv and then run pip install requests. Or if you were using a requirements.txt file you would add the desired package to requirements.txt, activate your virualenv, and run pip install -r requirements.txt

Now, with just one command, we add the package to our Pipfile and pipenv installs it immediately. We didn’t have to activate the virtualenv, just run the command from inside our project directory.

Install a specific version of a package

pipenv install Flask=='0.11.*'

This command will install the latest 0.11 version of flask and update the Pipfile to target this version.

Development only Packages

pipenv install --dev nose

pipenv install --dev pytest

Your project will likely use some development only packages for testing such as nose, pytest, tox, etc. The -dev flag will place these packages in a seperate [dev-packages] section of your pipfile.


Uninstall all packages

pipenv uninstall --all

Remove all packages from the virtualenv.

pipenv uninstall --all-dev

Remove all dev packages from the virtualenv.


Migrate from requirements.txt to Pipfile

pipenv install -r requirements.txt

This command will read in an existing requirements.txt file and populate your Pipfile for you. Simple!


Module Dependency Graph

pipenv graph

This will print all of your project’s installed module versions as well as the pip modules they depend on.


Update pip packages

After some time has passed we will want to upgrade to the latest versions of our installed pip packages. This is painless with pipenv, just type…

pipenv update

Here is what it looks like when you use pipenv to upgrade all of your pip packages.


Keeping your virtualenv inside your project directory

You might feel more comfortable storing your virtualenv inside your project directory. i.e.project/.venv No problem, pipenv supports this if you set the environment variable PIPENV_VENV_IN_PROJECT. Let’s try it.

If you would like to persist this behavior you can add the PIPENV_VENV_IN_PROJECT variable to your .bashrc file.

echo "export PIPENV_VENV_IN_PROJECT=1" >> ~/.bashrc


In Closing

I hope you enjoyed following along with this pipenv tutorial. Maybe it will save you some time and pain on your next python project.

Don’t forget, pipenv is the new way to manage package dependencies for python.
Goodbye requirements.txt, Hello Pipfile

References

More Python Goodness

Leave a Reply

Your email address will not be published. Required fields are marked *