Pipenv: A powerful blend of “pip” and “venv”
By: Vishal Sharma
Tags: Coding, Programming, Software Development, Python, Computer Science

Pipenv: A powerful blend of “pip” and “venv”

Bring all the packages to the Python world

pipenv > venv — Activate

Anyone who has worked on Python must have used “pip” and “venv” for their projects. Pip helps you to install a new library to your global Python environment.

Let’s say you are working on a new scraping project. But, you don’t have “Beautiful Soup" installed in your global environment. You can simply use the “pip” command to install it.

pip install beautifulsoup4

But, what if you are working on two projects and both of them require different versions of the same library?

pip becomes your foe and venv your only friend!

A virtual environment aka venv lets you install different dependencies of a package. So, you can use them in an isolated way.

It is fascinating that we can use “pip” if we are working on a single project and want to install a new package in the global environment. And, if we are working on different projects, a virtual environment works best for us.

Why I ditched venv and pip, then?

In PyCon 2108, Pipenv was introduced and was called as “The Future of Python Dependency Management”. Pipenv combines both “pip” and “venv” into one simple and easy-to-use tool.

Pipenv automatically creates and manages a virtual environment for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages.

Installing pipenv

For installing pipenv, just give the following command in your terminal.

pip install pipenv

Working with pipenv

Activating and Deactivating virtual environment

Now, if you do “pip 3 freeze” in your command line terminal. This will show everything you have in your current environment i.e global environment.

You can now create a new folder (wherever you want to build your project) and begin with pipenv journey. The following command will activate the environment.

pipenv shell

It will activate a new virtual environment for you. Also, it will create a pip file.

Now, if you open up Python and import the “sys” module. Using “sys.executable”, you will get to known where Python is being executed i.e in a virtual environment.

You can easily activate the environment using the “pipenv shell” command while deactivation is done using the “exit” command.

Installing packages with pipenv

You can simply install packages in your virtual environment using the following command.

pipenv install <package name>

It also creates a “Pipfile.lock”, which has all of the dependencies and sub-dependencies. And, it will give us a deterministic build so that we can use the same versions of everything when we are ready to deploy.

Now, if you open Python in the terminal. You can easily use the package you have installed before using the “pipenv install” command.

Listing packages in your environment

You can use the following command to list all the packages in your Python environment.

pipenv lock -r

Uninstalling Packages

If you do not want a package to be in your Python environment, you can simply uninstall it.

pipenv uninstall <package name>

Summary

I have talked about how you can get rid of pip and venv. Instead, you can work on a combination of both i.e pipenv. I have created a guide for pipenv usability. How you can install it? How you can work with it? Easy to use, scalable, and bringing both “venv” and “pip” tools together certainly makes pipenv tool the “Future of Python Dependency Management”.

References