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.
- If you work with colleagues on a project when you install a new package your college will be able get absolutely the same versions of all packages, so if code works for you it works for them also
- On the deployed server versions will same for you
How to install pipenv
sudo pip install pipenv
If the pip command is not found, you might have only pip3.
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.lock , please review them carefully. The
Pipfile.lock will contain a locked version of all packages.
You should always commit both
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:
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:
For me it prints:
If you need, read more about Pipenv advanced usage