[Python Tips] Are you still using Pip?

In a previous tip I talked about Virtual Environments and why you should be using them. While there are a few ways to do them, I want to talk more in-depth about my favorite.

Introducing pipenv

Pipenv is like a combination of Pip, VirtualEnv, and NPM although technically it's a combination of Pip & VirtualEnv.

PipEnv brings package.json type of functionality with the file Pipfile.

Pip makes it super easy to create virtual environments, switching to virtual environments, and running under a virtual environment without switching. It also manages your dependencies and gives you an easy way to look at your dependency graph.

Before you can use pipenv, you need to install it.

Pipenv installation

Pipenv is available on PyPi, so all you have to do is:

pip install -U pipenv

I recommend keeping pipenv global, so installing it directly is ideal.


Using pipenv

You can use pipenv similar to how you would use NPM to install packages and update the package file (in this case Pipfile).

One of the biggest features that most people don't even know about (I'm guessing they don't) is the additional security pipenv brings. Pipenv uses hashes everywhere and checks the file hashes for all locked dependencies.

Unlike VirtualEnv, pipenv does not create copies of modules and packages in your working folder.

Beem installed w/ VirtualEnv

Beem installed w/ Pipenv

Pipenv allows you to easily specify Python 2 or Python 3 when creating an environment.

Pipenv has two main commands for running your applications.

shell - To enter your environment
run - To run code without entering environment first

I'm not going to get into creating a Pipfile manually, Pipenv does a good job managing it automatically. There are some cases you might want to edit it by hand but that's beyond the scope of this tutorial.

Creating a virtual environment with pipenv

I'm going to skip this, because all you need to do is install a module and pipenv will start creating an environment for you.


Installing modules with Pipenv

Just like pip, all you do is

pipenv install <module>

If there is not a Pipfile, it will create one and start a new environment.

You can uninstall packages using pipenv uninstall.

You can use a requirements.txt file to load your dependencies similar to pip by using pipenv install -r requirements.txt.

You can then use pip install to install all packages from the Pipfile when you are ready to deploy are installing on a new machine.


Switching to your environment

Once you installed your packages, you can switch to your new virtual environment by using:

pipenv shell

This will enter the virtual environment, much like you do with VirtualEnv.

You can skip that all together and just run pipenv run python mycode.py arguments to just execute your code without switching first.


Updating Modules

You can easily upgrade modules by running pipenv update, pipenv will make the necessary changes to the Pipfile and Pipfile.lock for you.


Dev Packages

Like NPM, and unlike pip, you can install packages for development only. These are packages you need to do development (like linting software) but for the final deployment, they are not needed.

All you do is add --dev to the install command.

pipenv install --dev pylint

You can install all packages including dev packages using pip install --dev.


Dependency Graph

Pipenv has a really cool feature to look over the dependency graph of your application.

Just type pip graph and you will see something like this:

hivepy==0.9.999
  - appdirs [required: Any, installed: 1.4.4]
  - certifi [required: Any, installed: 2020.4.5.2]
  - ecdsa [required: >=0.13, installed: 0.15]
    - six [required: >=1.9.0, installed: 1.15.0]
  - funcy [required: Any, installed: 1.14]
  - future [required: Any, installed: 0.18.2]
  - langdetect [required: Any, installed: 1.0.8]
    - six [required: Any, installed: 1.15.0]
  - prettytable [required: Any, installed: 0.7.2]
  - pycrypto [required: >=1.9.1, installed: 2.6.1]
  - pylibscrypt [required: >=1.6.1, installed: 1.8.0]
  - scrypt [required: >=0.8.0, installed: 0.8.15]
  - toolz [required: Any, installed: 0.10.0]
  - ujson [required: Any, installed: 3.0.0]
  - urllib3 [required: Any, installed: 1.25.9]
  - voluptuous [required: Any, installed: 0.11.7]
  - w3lib [required: Any, installed: 1.22.0]
    - six [required: >=1.4.1, installed: 1.15.0]
PyYAML==5.3.1
requests==2.23.0
  - certifi [required: >=2017.4.17, installed: 2020.4.5.2]
  - chardet [required: >=3.0.2,<4, installed: 3.0.4]
  - idna [required: >=2.5,<3, installed: 2.9]
  - urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.9]

Really handy if you want an overview of your dependencies.


There are more features I suggest you check out if you have more questions. A good resource for pipenv can be found here.

If you are using Git, it is recommended to check in both Pipfile & Pipfile.lock.

As you can see, Pip & VirtualEnv are no match for Pipenv.

I have written a lot of Python tutorials, check them out!


My Python Tips Series


Securely chat with me on Keybase

Why you should vote me as witness

H2
H3
H4
3 columns
2 columns
1 column
9 Comments
Ecency