Ben S.
Ben S.

Reputation: 143

How to verify a group of modules is installed and up-to-date?

I've developed a script that other researchers in my group need to quickly churn through large amounts of data that we generate during experiments. However, I'm the only "Python guy" on our group and as such I can't always count on them to have up-to-date installations of modules like Numpy and Scipy.

Is there a way, in my script, to validate their installations of modules and prompt them to install/upgrade?

Upvotes: 2

Views: 68

Answers (2)

MSeifert
MSeifert

Reputation: 152677

You could use distutils.version.LooseVersion:

from distutils.version import LooseVersion
import numpy

if LooseVersion(numpy.__version__) < LooseVersion('1.10'):
    raise ValueError('upgrade numpy! You need version 1.10 to run this.')
    # or "print" instead of an exception.

If you don't know if the packages are installed you could also try to import the modules:

try:
    import numpy
except ImportError:
    raise ValueError("install numpy!")

Upvotes: 1

Christopher Apple
Christopher Apple

Reputation: 401

The way to do this is with a file called "setup.py". In this file, you can define all of the requirements for the script, and user's will automatically install them by calling: python setup.py install.

Some resources that may help:

Python docs on setup script: https://docs.python.org/2/distutils/setupscript.html

Example setup.py file: https://github.com/pypa/sampleproject/blob/master/setup.py

Another similar answer regarding setup.py: https://stackoverflow.com/a/4740640/7299836

Upvotes: 0

Related Questions