Setting up Pipenv

Christopher Af Bjur

June 20th, 2020

Pipenv is a tool which helps you manage your Python projects with more ease. Similar to Javascripts npm or yarn, Pipenv is a package manager.

Once you initialize Pipenv in your Python project, a Pipfile is created which will store information about the packages that you install and their version. This file will automatically be updated as you add/remove packages in your project. A Pipfile.lock file is also created which is used to produce deterministic builds. All of this might sound familiar if you're coming from Javascript, where you have a package.json that serves a similar purpose as well as .lock files.

Another great advantage of Pipenv is that it creates and manages the virtual environment for you.


Installation

You can install Pipenv by using pip:

Terminal

pip install pipenv

Note that if you're on a Mac and have installed Python 3+ you should use:

Terminal

pip3 install pipenv

If you're using Homebrew:

Terminal

brew install pipenv

Initializing Pipenv in Your Project

In your project root, run the following command to initialize Pipenv (our virtual environment).

Terminal

pipenv shell

This will create a Pipfile in our root folder as well as activate the virtual environment.

Our Terminal should now display that we're now in our virtual environment. If we want to jump out of it, we simply press CTRL + D.

If we want to activate our virtual environment again, we simply run this again:

Terminal

pipenv shell

Installing Packages

Next let's try to install a pypi package using Pipenv.

Terminal

pipenv install mysql-connector-python

Now this package has been installed to our virtual environment and we can also see that this package was added to our Pipfile.

CODICULUM

©2020-present Christopher af Bjur. All Rights Reserved.