Detect the minimum Python version to run code

In case we want to check if our code is compatible with a specific version of Python, we can get help from the library vermin.

Vermin is a library that detects the minimum Python version to run code.

As the documentation says:

“It functions by parsing Python code into an abstract syntax tree (AST), which it traverses and matches against internal dictionaries with 184 rules divided into 40 modules, 123 classes/functions/constants members of modules, and 21 kwargs of functions. Including looking for v2/v3 print expr and print(expr), "..".format(..), imports (import X, from X import Y, from X import *), and function calls wrt. name and kwargs.”

Here you can find some usage examples, which come from the github repo:

% ./vermin
Vermin 0.2.2
Usage: ./vermin [options] <python source files and folders..>

Options:
  -q      Quite mode. It only prints the final versions verdict.
  -v..    Verbosity level 1 to 2. -v shows less than -vv but more than no verbosity.
  -i      Ignore incompatible version warnings.
  -p=X    Use X concurrent processes to analyze files (defaults to all cores = 8).
  -d      Dump AST node visits.

% ./vermin -q vermin.py
Minimum required versions: 2.7, 3.0

% ./vermin.py -v examples
Detecting python files..
Analyzing 6 files using 8 processes..
             /path/to/examples/formatv2.py
2.7, 3.2     /path/to/examples/argparse.py
2.7, 3.0     /path/to/examples/formatv3.py
2.0, 3.0     /path/to/examples/printv3.py
!2, 3.4      /path/to/examples/abc.py
             /path/to/examples/unknown.py
Minimum required versions: !2, 3.4

When it yields !2 or !3 it means that it explicitly cannot run on that version

4 thoughts on “Detect the minimum Python version to run code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s