DEV Community

Cindy Achieng
Cindy Achieng

Posted on • Updated on

Pipenv, the new Python packaging tool

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?

  1. Think of pipenv as a powerful combination of pip and virtualenv.
  2. Goodbye to manually creating and managing you virtual environments pipenv does it for you!.
  3. Great at managing project's dependencies.Instead of pip's requirements.txt, pipenv creates two files the pipfile for the packages you installed directly and pipfile.lock for the dependencies and the specific version of the packages based on your pipfile.

pipenv workfow

  1. Create virtualenv if doesn't exist already.
  2. Create pipfile to manage the packages installed by you
  3. Finally, creates pipfile.lock to manage dependencies for the packages in pipfile

Let's get started!

We use pip to install pipenv,so ensure you have both python and pip installed.
To check if pip is installed run:

$ pip --version
Enter fullscreen mode Exit fullscreen mode

.. and python:

$ python --version
Enter fullscreen mode Exit fullscreen mode

Installing pipenv

$ pip install --user pipenv
Enter fullscreen mode Exit fullscreen mode

Note: I have used the user installation otherwise just run $ pip install pipenv.

Using 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.
Let's Initiate pipenv by using the install command.

$ mkdir laughing-blog //projectname
$ cd laughing-blog
$ pipenv --three install
Enter fullscreen mode Exit fullscreen mode

Note:Specify your python version by using $ pipenv --three for python 3 and $ pipenv --two for python 2.To use an abitrary python version do:
pipenv --python 3.6.4 or just $ pipenv install to 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 and Pipfile.lock in your project directory.

Below is a breakdown of the pipenv install output.

virtualenv

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
Enter fullscreen mode Exit fullscreen mode

pipfile

Creating a Pipfile for this project…
Enter fullscreen mode Exit fullscreen mode

Finally, pipfile.lock

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
Enter fullscreen mode Exit fullscreen mode

Activating Virtualenv

To activate virtualenv,simply run:

$ pipenv shell
Enter fullscreen mode Exit fullscreen mode

To exit virtulalenv,run:

$ exit
Enter fullscreen mode Exit fullscreen mode

Managing Packages

To install a package simply run:

$ pipenv  install <packagename>
Enter fullscreen mode Exit fullscreen mode

... and to remove the package in the pipfile run:

$ pipenv  uninstall <packagename> 
Enter fullscreen mode Exit fullscreen mode

understanding pipfile and pipfile.lock

Pipfile

Pipfile manages the packages that you install,think of it as an upgrade of requirements.txt.

What makes pipfile superior to requirements.txt

Lets look at a sample of requirements.txt file

Note:simply run pip freeze > requirements.txt to 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
...
Enter fullscreen mode Exit fullscreen mode

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 pipfile.lock

pipfile.lock

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
Enter fullscreen mode Exit fullscreen mode

pipfile sample

 [packages]
 django = "*"
 django-allauth = "*"

 [requires]
 python_version = "3.5"
Enter fullscreen mode Exit fullscreen mode

pipfile.lock sample

  "django-allauth": {
"hashes": [
"sha256:7b31526cccd1c46f9f09acf0703068e8a9669337d29eb065f7e8143c2d897339"
],
"index": "pypi",
"version": "==0.35.0"
},
"idna": {
"hashes": [
"sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f",
"sha256:8c7309c718f94b3a625cb648ace320157ad16ff131ae0af362c9f21b80ef6ec4"
],
"version": "==2.6"
},
"oauthlib": {
"hashes": [
"sha256:09d438bcac8f004ae348e721e9d8a7792a9e23cd574634e973173344046287f5",
"sha256:909665297635fa11fe9914c146d875f2ed41c8c2d78e21a529dd71c0ba756508"
],
"version": "==2.0.7"
},
"python3-openid": {
"hashes": [
"sha256:0086da6b6ef3161cfe50fb1ee5cceaf2cda1700019fda03c2c5c440ca6abe4fa",
"sha256:628d365d687e12da12d02c6691170f4451db28d6d68d050007e4a40065868502"
],
"version": "==3.1.0"
},
Enter fullscreen mode Exit fullscreen mode



Conclusion

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

Top comments (3)

Collapse
 
lyjoker profile image
Momen Fathi

is pipenv very comfortable with Windows?

Collapse
 
cindyachieng profile image
Cindy Achieng • Edited

Hello,from the info I have gathered, yes it is!However,I haven't tested it on Windows yet.I will update the post for window users and give a user based opinion on the same.

Collapse
 
lyjoker profile image
Momen Fathi • Edited

please do, thanx and good luck! ;)