Bharat Kalluri     ·  About

Pipenv is awesome!

Creating virtual environments was a tedious job. To create a virtual environment, they had to use packages like virtualenv and pip inside the virtual environment. It was not a very efficient way of handling things, and now pipenv changed everything!

Why do we need a virtual environment?

This was a question I had when I first started programming. Every tutorial I used to go through first made us install virtualenv and create a virtual environment.

Later I came to understand why it was important. Let us consider a scenario where you are developing a web application for a client using Django 1.7. And still while this application was in development, another project started and you decided you will use Django 1.8. Let us suppose 1.8 is not completely backward compatible ie. It breaks some functions in 1.7. So if you upgrade Your old project will no longer function. Virtual environments come to help in this case. You will just have to create two isolated environments for two projects and you will no longer have package problems!

How virtual environments were created before

To manage virtual environments, everybody used virtualenv. It is used to create isolated environments for different projects.

To install virtualenv

pip install virtualenv

Now, using virtualenv, let’s create a project

virtualenv projectA

Now, activate the newly created environment by

source projectA/bin/activate

Now, you must notice a change in the prompt. After this, we can start installing packages directly

pip install Django

If you want to make sure everyone else on your team is using the same packages as you are, then simply do

pip freeze > requirements.txt

To come out of the virtual environment, just do

deactivate

And then pipenv changed everything!

So what does pipenv offer? Pipenv is a higher level abstraction. It uses virtualenv to create and manage environments. But with pipenv getting started is really easy. Let us do the same task as above in pipenv.

Let us first install pipenv.

pip install --user pipenv

The user flag makes sure the package is installed for just the user. You don’t need admin privileges for executing this command.

To create a project, simply create a directory and change into it and install Django!

pipenv install Django

Essentially, we just skipped creating and activating the environment because pipenv takes care of that for us. Another cool feature is that every time we install some thing, pipenv adds it to a pipfile, this is something like requirements.txt and keeps track of all dependencies.

Now to run our program inside the virtualenv, we just have to do

pipenv run python main.py

If you want to go into the shell for the virtualenv, just type

pipenv shell

Pipenv makes everything seamless and easy. It even is the recommended package for creating virtual environments for python!! So if you are not using pipenv, you are missing out!

Written February 17, 2018.

← Managing dotfiles using GNU Stow  ELI5: Joins in Sql →