Proper way to automatically install external Python modules

I am developing a Scripted Loadable Module that utilizes several external Python modules, specifically scipy, sklearn, and PyWavelets.

Currently, the module has the below script written along with its import statements:

pip_modules = ['scipy', 'sklearn', 'PyWavelets']
for module_ in pip_modules:
    try:
        module_obj = __import__(module_)
    except ImportError:
        logging.info("{0} was not found.\n Attempting to install {0} . . ."
                     .format(module_))
        pip_main(['install', module_])

However, I feel this is an ugly and perhaps unacceptable way to do this.
Is there a proper or preferred “Slicery” way for me to check if these modules exist and install them if they do not?

The main approach I have been looking at was using the cmake files, but I have not had any success in that area.

1 Like

This solution is appropriate for the current level of support for custom Python dependencies.

Once pip install works for all Python packages on all platforms (currently, binary packages are not available on Windows), we can add some Slicer infrastructure to manage package dependency in a nice, abstract way. Slicer needs to switch to Python3 first, so it will take at least 6-12 months from now.

1 Like