How To Use Pipenv To Manage Your Python Projects

  • codedamn official's profile image
    codedamn official
How To Use Pipenv To Manage Your Python Projects

Pipenv is a package management tool for python just like npm/yarn for NodeJS. In fact, Pipenv is the official package management tool recommended by Python itself for managing package dependencies. Whenever you start with a new python project, let’s say a Django project, you would not want to clutter the global package space in your PC. Global installation of packages won’t allow you to work with multiple Django projects or with different versions of it. Also, it is necessary to track the version of Django so if there is any bug/feature update, you can update it and in case, the update is crashing your app, you can go to the previous stable version. Pipenv does all this management and complex handling of all dependencies under the hood for you.

Why Pipenv

First of all, let’s see why you should use Pipenv. Pipenv solves a large number of critical issues. You need not use pip and other virtual environment tools like virtualenv separately. It automatically creates and manages virtual environments for your projects. No need to use and manage dependencies with the requirements.txt file, instead, Pipfile will manage it for you, and Pipfile.lock will ensure deterministic builds (with secure hashes) for production. It also helps with tracking complex interdependencies in the project, so even in the case of conflicting dependencies, Pipenv takes the pain away from you. It automatically loads the .env (environment variables) file to the virtual environment shell of Pipenv but note that it should be in the root directory of the project folder.

It’s also a great thing that it doesn’t create a virtual environment folder (where it install packages) in the project folder rather creates it in the .virtualenvs folder in your system users folder. Thus, the project does not get clutter with packages and it looks much cleaner and easier to work on.

Now that you have seen so many advantages of using Pipenv, let’s go deep into it and see how to use it.

How To Install Pipenv

Install pipenv globally simply by using the pip tool that comes with python,

pip install pipenv

After it’s installed, you can use pipenv keyword directly in your terminal.

Create & Manage Python Projects

Now to create python projects, create a folder of your project name and open the terminal inside it. Just use pipenv install and then the name of the package you want to install. For eg.

cd my-project
pipenv install requests

Install dev dependencies like testing packages with –dev flag,

pipenv install pytest –dev

You can also initiate it manually with a custom python version by :

pipenv –python 3.7

Pipenv instantiates automatically and will create two files in the folder – Pipfile and  Pipfile.lock. Pipfile includes the list of all the top-level packages you’ve installed (packages that you would actually import and use, not their sub-dependency) and Pipfile.lock contains the exact versions of all dependencies including the sub-dependencies along with their hashes for secure and deterministic build on production.

By the way, you can also use the requirements.txt file if you have one, to install project dependencies,

pipenv install requirements.txt

To update all your packages to the latest version,

pipenv update

OR for specific package update.

pipenv update <package>

To uninstall package(s)  you no longer need,

pipenv uninstall numpy

And to uninstall all packages at once,

pipenv uninstall –all

Run Your Code

You can enter or use the virtual environment by two ways. First, by using shell keyword to go to virtual environment shell,

pipenv shell
python file.py

Second, by directly writing shell commands for virtual environment without entering into it, after run keyword.

pipenv run python file.py

After you have completed your project and ready for production, lock the development environment so that the exact environment can be produced on production, by

pipenv lock

In production, use –ignore-pipfile flag to install packages and all dependencies from Pipfile.lock instead of Pipfile,

pipenv install –ignore-pipfile

OR

pipenv sync

Other Useful Commands

To list the dependency graph of your installed dependencies, write

pipenv graph

To show your project home folder,

pipenv –where

To show the location of the virtual environment folder linked with the project,

pipenv –venv

To uninstall all packages that are not specified in Pipfile.lock,

pipenv clean

To check for security vulnerabilities and against PEP 508 markers provided in Pipfile,

pipenv check

Use –rm flag to remove the virtual environment folder associated with the project where all the packages are installed.

pipenv –rm

For more commands and details, visit Pipenv documentation.

Conclusion

I hope you’d have got a brief overview of Pipenv along with its main useful commands. Create, manage, track, share your python projects easily and reproduce the exact environment anywhere. Deploy with ultimate peace of mind!

If you want to ask something, connect with me on Twitter and LinkedIn.