Article Image
read

If you’ve never used pip to install a package you love how simple it is, pip install <pkgName>1

For this short post I’ll use live output for the Ansible package upgrade, because hey I need to upgrade it anyway and I always forget the right incantation with pip. ☺️

Using pip is pretty straightforward however, if you’re a casual user like me it’s handy to have a quick and simple walk-through. So here we go.

First up, if you’re on macOS you’ll want to sudo -H these pip terminal commands.

Update PIP

So, this is surprisingly important make sure pip is up-to-date or at least at the minimum version required by the package you are updating.

To check the version simply use: pip --version and you see somthing like this:

dev $ pip --version
pip 9.0.1 from /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)

Of course, you can skip the version check and just go straight to the latest version of pip using:

pip install --upgrade pip

On macOS Mojave, you’ll see something like this:

    dev $ sudo -H pip install --upgrade pip
    Password:
    Collecting pip
      Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
        100% |████████████████████████████████| 1.3MB 1.1MB/s 
    Installing collected packages: pip
      Found existing installation: pip 9.0.1
        Uninstalling pip-9.0.1:
          Successfully uninstalled pip-9.0.1
    Successfully installed pip-18.1

Yes, I know, don’t @ me about the old version of pip, it’s a new laptop 😜

Upgrade the Package

The astute among you will have noticed pip’s upgrade option was used to upgrade pip, but before you jump in there’s a few things to consider.

Like everything else the pip the install command has some nice features and options to consider.

The standard upgrade will upgrade all dependencies to their latest version as well as your target package in pip terminology this an eager upgrade.2

sudo -H pip install <pkgName> --upgrade

    dev $ sudo -H pip install ansible --upgrade
    Password:
    Collecting ansible
      Downloading https://files.pythonhosted.org/packages/56/fb/b661ae256c5e4a5c42859860f59f9a1a0b82fbc481306b30e3c5159d519d/ansible-2.7.5.tar.gz (11.8MB)
        100% |████████████████████████████████| 11.8MB 1.4MB/s 
    Requirement already satisfied, skipping upgrade: jinja2 in /Library/Python/2.7/site-packages (from ansible) (2.9.6)
    Requirement already satisfied, skipping upgrade: PyYAML in /Library/Python/2.7/site-packages (from ansible) (3.12)
    Requirement already satisfied, skipping upgrade: paramiko in /Library/Python/2.7/site-packages (from ansible) (2.2.1)
    Requirement already satisfied, skipping upgrade: cryptography in /Library/Python/2.7/site-packages (from ansible) (1.9)
    Requirement already satisfied, skipping upgrade: setuptools in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from ansible) (18.5)
    Requirement already satisfied, skipping upgrade: MarkupSafe>=0.23 in /Library/Python/2.7/site-packages (from jinja2->ansible) (1.0)
    Requirement already satisfied, skipping upgrade: pyasn1>=0.1.7 in /Library/Python/2.7/site-packages (from paramiko->ansible) (0.2.3)
    Requirement already satisfied, skipping upgrade: pynacl>=1.0.1 in /Library/Python/2.7/site-packages (from paramiko->ansible) (1.1.2)
    Requirement already satisfied, skipping upgrade: bcrypt>=3.1.3 in /Library/Python/2.7/site-packages (from paramiko->ansible) (3.1.3)
    Requirement already satisfied, skipping upgrade: idna>=2.1 in /Library/Python/2.7/site-packages (from cryptography->ansible) (2.5)
    Requirement already satisfied, skipping upgrade: enum34 in /Library/Python/2.7/site-packages (from cryptography->ansible) (1.1.6)
    Requirement already satisfied, skipping upgrade: cffi>=1.7 in /Library/Python/2.7/site-packages (from cryptography->ansible) (1.10.0)
    Requirement already satisfied, skipping upgrade: asn1crypto>=0.21.0 in /Library/Python/2.7/site-packages (from cryptography->ansible) (0.22.0)
    Requirement already satisfied, skipping upgrade: ipaddress in /Library/Python/2.7/site-packages (from cryptography->ansible) (1.0.18)
    Requirement already satisfied, skipping upgrade: six>=1.4.1 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from cryptography->ansible) (1.4.1)
    Requirement already satisfied, skipping upgrade: pycparser in /Library/Python/2.7/site-packages (from cffi>=1.7->cryptography->ansible) (2.18)
    Installing collected packages: ansible
      Found existing installation: ansible 2.3.1.0
        Uninstalling ansible-2.3.1.0:
          Successfully uninstalled ansible-2.3.1.0
      Running setup.py install for ansible ... done
    Successfully installed ansible-2.7.5

Nine times out of ten, this is probably the option you want, however, pip install --upgrade has an option called --upgrade-strategy with two possible settings.

The default value is only-if-needed from 10.0 onwards (which you should be on if you took my advice and updated pip). So, by default the --upgrade-strategy only-if-needed is set and pip will only update dependencies that don’t meet the minimum requirements of the package you are updating.3

The other option is eager and prior to 10.0 was the default setting. It was changed due to the breaking nature of eager when an upgrade had conflicting dependencies. So, while there could be a case to use eager just consider what you might break if you use this option.

Photo by freestocks.org from Pexels


  1. In the examples anywhere you see <pkgName> you can replace it with the name of package you’re working with. 

  2. This is true for versions prior to 10.0… keep reading for the current state of affairs. 

  3. Please note that any dependency package that is updated will be updated to it latest stable release within the requirements of the primary package you’re updating. 

Blog Logo

CPPL

The sum of it.


Published

Image

The Sum Of It

A place of for me to write things others may be interested in or find useful.

Back to Overview