We will be exploring the Python package management tool Pipenv. It is a dependency management tool in python. It is a high-level tool for managing dependency inside a Python project Pipenv is Python’s recommended package manager. It is simple and easier to use as compared to pip and virtual environment combined.
Pipenv creates a virtual environment separately for each initialization of the project. It is by default located in a specific folder but that can be configured, we will dive into this in this article later.
Installation
Pipenv is itself a python package so, we can simply install it globally with pip. To install pipenv, enter the following command in the terminal or cmd.
pip install pipenv
If this doesn’t work for you, install pipenv as you install any other package using pip in your system.
We have downloaded the pipenv globally on our system, so we can use it in all our projects wherever we need to.
Setting up Pipenv
Once we have pipenv installed in the environment, we can use pipenv specific commands to install packages or create a new virtual environment. To simply, Create a virtual env with pipenv we can run the following command:
pipenv shell
So, we can see it created a virtual env somewhere and activated it. The activation is indicated by the name of the venv at the beginning of the prompt. This is the command to instantiate a simple virtual environment for the current project.
The above command is equivalent to this cmd
virtualenv venv
Windows: venv\Scripts\activate OR Linux: source venv/bin/activate
Understanding Pipfile
If you look at the Pipfile generated by the pipenv tool, you will see it is more in detail than the regular requirements.txt file. This also stores more information than that. It stores the python version along with the version of the packages which are used and which are dependent on a specific version. It also stores the source of the resource from which the packages were downloaded.
But, there are cases we need a simple text file usually called the requirements.txt file to install from pip. So, we can use Pipenv to convert the automatically generated Pipfile into a simple text file called requirements.txt file for listing the dependencies in the project. But before that Let’s install one package for a reference.
Installing python packages
We can now simply install any package with pipenv or pip. If you are in the activated virtual environment of pipenv, you can simply install pip, or else you need to use pipenv in order to install the dependencies for the current project.
Pipenv Pipfile to requirements.txt file
So, let’s say we have a project with some dependencies like pandas or any. We have pandas as a dev dependency. So, if we look at the Pipfile, we can see a text file like the following in C:\Users\<user_name>:
So, suppose we have a pipenv setup in a Python project and we want to get a requirements.txt file. Pipfile is great and more informative but there might be instances where you need a simple text file to install dependencies. We’ll see how to convert the Pipfile into a requirements.txt file
pipenv lock -r > requirements.txt
This will generate a requirements.txt file with the following content:
If you want a simple file just like the pip freeze command, you can run the following command as well:
pipenv run pip freeze > requirements.txt
The drawback here is that it also adds the dev dependencies as it is adding the dependencies with the help of pip and not Pipenv. So, that is how we are able to convert a Pipfile in pipenv into a simple requirements.txt file
As we can see that the dependency is installed as a dev-dependency. We can uninstall the dev-dependency at any time in the project with the command:
pipenv uninstall --all-dev
So, we can see we can more easily manage dependencies in python with pipenv. We can also uninstall all the dependencies with the command:
pipenv uninstall --all