Clearly,I have been living under a rock but not anymore! I shared an article about setting django environment using pip and virtualenv and M. Floering suggested I check
pipenv. You guessed right , yes I did and I am loving it!
Pipenv is the official recommended python packaging tool acccording to pipenv.org.
It is a great tool for managing your project's dependencies as well as install and uninstall packages.
Seems like something pip and virtualenv did just well, but why pipenv?
- Think of
pipenvas a powerful combination of pip and virtualenv.
- Goodbye to manually creating and managing you virtual environments
pipenvdoes it for you!.
- Great at managing project's dependencies.Instead of pip's
pipenvcreates two files the
pipfilefor the packages you installed directly and
pipfile.lockfor the dependencies and the specific version of the packages based on your pipfile.
- Create virtualenv if doesn't exist already.
pipfileto manage the packages installed by you
- Finally, creates
pipfile.lockto manage dependencies for the packages in pipfile
pip to install
pipenv,so ensure you have both python and pip installed.
To check if pip is installed run:
$ pip --version
.. and python:
$ python --version
$ pip install --user pipenv
Note: I have used the user installation otherwise just run $ pip install pipenv.
Now that we have successfully installed
pipenv, let's use it. Create the project directory and name it(I will call mine laughing-blog) and then change directory into the project's folder.
pipenv by using the
$ mkdir laughing-blog //projectname $ cd laughing-blog $ pipenv --three install
Note:Specify your python version by using
$ pipenv --threefor python 3 and
$ pipenv --twofor python 2.To use an abitrary python version do:
pipenv --python 3.6.4or just
$ pipenv installto use the default python version.
The above code creates a new virtual environment for your project if it doesn’t exist already and the two magic files,
Pipfile.lock in your project directory.
Below is a breakdown of the pipenv install output.
Creating a virtualenv for this project… Using /usr/bin/python3 (3.5.2) to create virtualenv Virtualenv location: /home/cindy/.local/share/virtualenvs/laughing-blog-FpALE3CM
Creating a Pipfile for this project…
Pipfile.lock not found, creating… Locking [dev-packages] dependencies… Locking [packages] dependencies… Updated Pipfile.lock (711973)! Installing dependencies from Pipfile.lock (711973)… 🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate virtualenv,simply run:
$ pipenv shell
To exit virtulalenv,run:
To install a package simply run:
$ pipenv install <packagename>
... and to remove the package in the pipfile run:
$ pipenv uninstall <packagename>
Pipfile manages the packages that you install,think of it as an upgrade of
What makes pipfile superior to requirements.txt
Lets look at a sample of
pip freeze > requirements.txtto generate the above file
... certifi==2018.4.16 chardet==3.0.4 defusedxml==0.5.0 Django==2.0.4 django-allauth==0.35.0 idna==2.6 oauthlib==2.0.7 python3-openid==3.1.0 ...
You will notice that
requirements.txt list all the package we installed , version and the dependencies as well.
pipfile holds the packages we installed and spare the rest to
This is the file that contains the dependencies based on the packages present in
Pipfile, the specific version of the packages to be used avoiding the risks of automatically upgrading packages that depend upon each other and breaking your project dependency tree.
If pipfile.lock wasn't created for some reason, you can lock the currently installed packages by running:
$ pipenv lock
[packages] django = "*" django-allauth = "*" [requires] python_version = "3.5"
Now,you know the definition,advantages and how pipenv functions.And the difference between the two magic files pipfile and pipfile.lock.
This Post was originally posted on achiengcindy.com