What is pipenv and how to install it

Pipenv is another tool for installing python packages.

It does the same as pip: just installs the packages. But the main difference - it creates a virtual environment for your new project, so it guarantees versions of packages and dependencies will not clash between different projects.

Also, pipenv environment tiered to one python version, you can simply create one project for python 3.3, and another for python3.6. Then when you will run python, it will use the right version from pipenv environment.

Another great advantage: pipenv freezes version numbers of all packages: Major+minor+patch. When native pip's requirments.txt can hold unpredictive version and it is easy to forget to modify the file before pushing.

This means:

How to install pipenv

Just run:

sudo pip install pipenv

If the pip command is not found, you might have only pip3.

After this, pipenv the command should start working in the terminal.

How to work with it

To create a new project, create a folder that will contain a source code (for example it might be a folder that soon will be a GitHub repository root)

pipenv --python 3.6

The python version which you specify should be installed. Here is how to install a specific python version.

At this point, pipenv created all needed files and a virtual env for your packages (It is a dedicated folder in your filesystem, outside of the project, so you will not able to accidentally push all these heavy dependency packages to your GitHub).

Now to install some package to your project:

pipenv install requests

After this command, the request package will be added to special Pipfile and Pipfile.lock , please review them carefully. The Pipfile.lock will contain a locked version of all packages.

You should always commit both Pipfile and Pipfile.lock to your git repository, otherwise your team or you might face LockfileNotFound("Pipfile.lock")!

Once you installed a new package to your project, just commit, and ask your co-developers to run:

pipenv sync

So they will automatically create a virtual environment if they executed the command the first time. Also, it will install all the dependencies.

To run python in your environment use:

pipenv run python yourfile.py

So it will use your python3.6, even if the default system's python is 3.8.

If it is some specific framework command, just always prepend pipenv run ! For example, if you have Django installed in pipenv, run:

pipenv run django-adminย help

Where packages in installed

To understand where pipenv creates a virtual environment filesystem with site-packages folder:

pipenv --venv

For me it prints:

/home/ivan/.local/share/virtualenvs/Ivan-dHPd5gk0

If you need, read more about Pipenv advanced usage

Environment view

#pipenv #pip #python
0
Ivan Borshchov profile picture
May 31, 2021
by Ivan Borshchov
Did it help you?
Yes !
No

Best related