1

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.

0

2 Answers 2

4

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])
Sign up to request clarification or add additional context in comments.

2 Comments

How can I do this if I do not want to import it? Can I Just throw it out?
That is very difficult (stackoverflow.com/questions/1668223/…). At least you could remove the import after the exception as this is not required. I have modified the answer to use imp to check if the module is available which does not actually import the module
-3

you should use this

import pip
import sys

def install(package):
    if not package in sys.modules:
        pip.main(['install', package])
# Example
if __name__ == '__main__':
    install('argh')

1 Comment

if package just tests whether package isn't an empty string - why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.