Python

Easy Python Module Version Management

Python comes with a handy-dandy little tool called pip for installing modules that you import in scripts. Python3 comes with a hand-dandy little tool called pip3 for installing modules you import in scripts. A practice I try to follow is to take the version of a module I was using when I wrote a script and create a requirements.txt file alongside it.

Basically, if I import then I’ll add what I imported into the requirements.txt file. This would usually follow the below syntax:

tensorboard==2.0.2
tensorflow==2.0.0
tensorflow-estimator-2.0.1
werkzeug-0.16.0
wrapt-1.11.2

Now that I have a requirements file, I can use that to install packages on the next host I run the script on:

pip install -r requirements.txt

But what if I need to make sure that I don’t update beyond those versions. I can use the freeze verb in pip:

pip freeze > requirements.txt


If you end up with a bad install or one that isn’t working properly, you can always uninstall the module.

pip uninstall pandas


You can then install verbosely, using the URI of the project. For example:

pip install -Iv http://www.github.com/krypted/URI-Of-Project

The above will produce verbose logs so that if it’s failing because of a weird reason you can case that down.

From Bryson: If you do a “`pip uninstall ${package}“` you may (usually will) end up with a lot of other packages that aren’t at the desired version.

Example: `requests` has its own requirements including `urllib3`. If you meant to install an older version of requests, and uninstalled it, you’re only removing that package and none of the sub-requirements that came along with it. They’re still there. When you go to re-install requests at the older version, the newer version of urllib3 that you had installed likely still meets the version required by requests. `pip` doesn’t handle dependency trees, so you need to be careful that you’re cleaning up additional packages.

In almost all cases it’s better to spin up an independent Python environment (a virtual environment) to install packages into that can be considered disposable. You can delete the entire venv and reinstall all your dependencies if there were any incorrect versions.

From Nick: On macOS, especially if you install python from different/multiple sources (such as brew, Python.org, etc), `pip` on the command line may not actually point to the Python you expect, based on order in the path.
It’s helpful to use the absolute path for pip to help alleviate that confusion. Additionally, it’s generally not recommended to mess with the System (Apple-provided) Python, because it tends to make permissions complicated when installing user vs. system level modules.

Do you have other tips or tricks? Drop a comment and I’ll try and add them!