Pipenv to Manage Python Packages and Virtual Environments
Apr 04, 2019 | Updated: Apr 04, 2019

As a developer you should have separate virtual environments for every Python based project so that project-wise dependencies can be managed separately. Pipenv is a Python's officially recommended tool for managing Python packages and virtual environments.

In this article I will show how to install Pipenv and discuss some basic usage of Pipenv.


Install Pipenv using Pip

I assume you already have Python installed in your computer. If not then install it first. For detail instructions of installing python check the following articles:

To install Pipenv you also need to make sure that you have Pip installed. Most likely with Python installation you should have Pip already available in your computer. To check this run the following command in your terminal and you should see the Pip version.

$ pip --version

Now run the following command to install Pipenv using Pip.

$ pip install pipenv


Install Pipenv using Homebrew

This section is specifically for Mac user who want to install Pipenv via Homebrew (The missing package manager for Mac OS). It makes the installation process easier and simpler for installing any software packages that Apple didn't.

The good thing of installing Pipenv via Homebrew is that it keeps Pipenv and all of its dependencies in a separated virtual environment, and hence it never mess with the rest of your Python installation. That's cool!

To install Pipenv via Homebrew simply run the following command in your terminal.

$ brew install pipenv


Basic usage of Pipenv

In this section I have listed some basic usage of Pipenv. For a complete list of commands and options visit the Pipenv official docs.

To install a specific package for your project run the following command (package will be added to Pipfile automatically).

$ pipenv install packagename

To install a specific package for your project as a dev dependency run the following command (package will be added to Pipfile automatically).

$ pipenv install packagename --dev

To activate a project's virtualenv, run the following command.

$ pipenv shell

To deactivate a project's virtualenv or exit from a project's virtualenv, type $ exit and hit Enter key or press Ctrl + D together.

To uninstall a specific package from your project run the following command (package will be removed from Pipfile automatically).

$ pipenv uninstall packagename

To uninstall all packages from your project run the following command (packages will be removed from Pipfile automatically).

$ pipenv uninstall --all

To install all dependencies (from Pipfile) for a project (without dev dependencies) run the following command.

$ pipenv install

To install all dependencies (from Pipfile) for a project (including dev dependencies) run the following command.

$ pipenv install --dev

To remove virtualenv of a project run the following command.

$ pipenv --rm

To locate the home of a project run the following command.

$ pipenv --where

To locate the virtualenv of a project run the following command.

$ pipenv --venv

Once you have your local development completed and now you are ready to push it to production, then you have to update the Pipfile.lock by running the following command. This way, you can ensure that production will have the same environment like your local development.

$ pipenv lock

Now to install the project dependencies to production environment run the following command. It informs Pipenv to ignore Pipfile but install packages from Pipfile.lock .

$ pipenv install --ignore-pipfile


Uninstall Pipenv

To uninstall Pipenv using Pip run the following command in your terminal.

$ pip uninstall pipenv

To uninstall Pipenv using Homebrew run the following command in your terminal.

$ brew uninstall pipenv


Summary

As Pipenv is a officially recommended tool for managing Python packages and virtual environments, it's better to switch over Pipenv from pip, virtualenv or venv. The good thing about Pipenv is that it combines the functionality of pip and virtualenv with a single command line interface in a simplified way. That's awesome!

Happy Python coding!


References


Comments

You are welcome to write comments, suggestions, corrections, or any queries related to the article. Your comments may take some time to be appeared. Please be aware that any irrelevant comments will be deleted. Thanks for your understanding, and your respectful & relevant comments!