Of course I have a backup!

Random blobs of wisdom about software development

The recommended python package manager, pipenv

Sunday, February 04, 2018

About one and a half year ago, I wrote about the shortcomings of the python package manager, pip. Finally, there is now an officially recommended package manager, called pipenv, that solves all of the problems mentioned there, and providers a few extra goodies too.

Main features of pipenv

  • Uses pipfile instead of the old requirements.txt
  • Supports dev packages by default
  • Automatically generates a lockfile, providing deterministic builds
  • Automatically creates and manages a virtualenv for your project
  • Only records top level dependencies in the Pipfile
  • Many other goodies

How does the automatic virtualenv work?

When installing the first package into a project, pipenv will create a virtualenv in your ~/.virtualenv folder, and tell you the (autogenerated) path. You don't ever need to manually enter the virtualenv, because pipenv providers two commands:

  • pipenv run myscript.py for running something inside the virtualenv
  • pipenv shell for opening a shell with the virtualenv activated

Other goodies

Using environment variables for per-environment settings is very common, so common that pipenv providers automatic loading for them. If you have a file called .env in your project root, pipenv run, and pipenv shell will automatically source it.

You can use pipenv graph for seeing your dependency graph, it will look something like this:

feedparser==5.2.1
Flask==0.12.2
  - click [required: >=2.0, installed: 6.7]
  - itsdangerous [required: >=0.21, installed: 0.24]
  - Jinja2 [required: >=2.4, installed: 2.10]
    - MarkupSafe [required: >=0.23, installed: 1.0]
  - Werkzeug [required: >=0.7, installed: 0.14.1]

You can also use pipenv check for checking for packages with known security issues. You can use pipenv install --dev packageX for installing dev packages. These will not be installed with a normal pipenv install invocation.

The output of pipenv install packageX is just so much nicer:

$ pipenv install feedparser
Creating a virtualenv for this project…
⠋Using base prefix '/usr'
New python executable in /home/myuser/.virtualenvs/myrss-cuHRAU_K/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /home/myuser/.virtualenvs/myrss-cuHRAU_K
Creating a Pipfile for this project…                                                                                                                                                                                                                                                                                          Installing feedparser…
Collecting feedparser
  Downloading feedparser-5.2.1.zip (1.2MB)
Building wheels for collected packages: feedparser
  Running setup.py bdist_wheel for feedparser: started
  Running setup.py bdist_wheel for feedparser: finished with status 'done'
  Stored in directory: /home/myuser/.cache/pip/wheels/15/ce/10/b500f745822ea6db6ea8ed225c06b15c000d71016b89ef9037
Successfully built feedparser
Installing collected packages: feedparser
Successfully installed feedparser-5.2.1

Adding feedparser to Pipfile's [packages]…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (5b437e)!

It also supports importing your old requirements.txt file, so there is really no reason not to start using it right away.

This was written by Norbert Kéri, posted on Sunday, February 04, 2018, at 15:03

Tagged as:

Post a comment

Providing your email is optional, it is never published or shared, it is only used for auto approval purposes. If you already have at least 1 approved comment(s) tied to your email, you don't have to wait for moderation, otherwise the author must approve your comment.

Please solve this totally random captcha