Search code examples
pythonpluginspip

Using pip to install a program while running


I have a program with plugin functionality and I don't want to install all the plugins from setup.py if I am not using them. They should only be installed when they are activated in the config of the main program.

Is it possible to install these programs using pip while running the main program?

Something like this:

try:
   if PLUGINNAME not installed:
      pip install PLUGINNAME

I know its possible to use os.system to force console input but that seems really bad.


Solution

  • You can do this with a by trying to import the module. If the module is not installed an ImportError will be given and you can install the package.

    import pip
    import imp
    
    try:
        imp.find_module(package)
    except ImportError:
        pip.main(['install', package])