Pipenv rolls the management of Python virtual environments and Python packages into a single tool. Pipenv ensures that each project uses the correct version of each package it needs, and that each of those packages has the correct dependencies as well.
Further, Pipenv generates a list of your project’s dependencies that can travel with it, allowing other users or developers to set up the same project in the same way. Other users will also need to install Pipenv to properly set up a Pipenv-managed project, but fortunately, installing and using Pipenv is a breeze.
Begin by using pip to install Pipenv and its dependencies,
pip install pipenv
Then change directory to the folder containing your Python project and initiate Pipenv,
cd my_project pipenv install
This will create two new files, Pipfile and Pipfile.lock, in your project directory, and a new virtual environment for your project if it doesn’t exist already. If you add the –two or –three flags to that last command above, it will initialise your project to use Python 2 or 3, respectively. Otherwise the default version of Python will be used.
When you install a package with Pipenv, two things happen. First, Pipenv will check if a virtual environment has already been created for this project directory. If yes, Pipenv will install the package into the existing virtual environment. If no, Pipenv will create a virtual environment that uses the same edition of Python used to run Pipenv. Note that the virtual environment is not created in the project directory itself; it’s created in a directory managed by Pipenv in your user profile.
Second, Pipenv will install the requested packages to the virtual environment. When the installation is done, Pipenv will report back on all that it did, including a path to the virtual environment if it had to create one.
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.
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.
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.