3

I have this simple Python script. I want to include some sort of condition that checks for the Python module (in my example below subprocess) before running it. If the module is not present, install the module then run the script. If the module is present, skip the installation of the module and run the script. I am struggling with most of the similar scenarios I am seeing online.

import subprocess
ls_output = subprocess.check_output(['ls'])
print ls_output
5
  • 1
    subprocess is built in to 2.7 and 3.x. It was added to 2.4. You should not try to install it on anything 2.4 or newer. Commented Sep 20, 2016 at 1:39
  • This sounds like a suspiciously wrong thing to want to do. You should probably have the library you need installed listed as a dependency of your application. It will then be installed when your application is installed. In order to install a library your script would require super user access, which is a security risk. Aside from that which method of installation would you use? pip, conda, git checkout & setup.py? Also would you use a virtual env? Commented Sep 20, 2016 at 1:50
  • I was just using subprocess as an example to simplify my question. The actual module is pycurl. I want the script to run only if pycurl is installed. And if its not installed, then install via pip then execute. Commented Sep 20, 2016 at 2:10
  • Thanks Paul- The script is a simple script running on my linux server. I just want to check if pycurl is installed, if not, install pycurl, import and then run the script. If pycurl is already installed, import pycurl, then run the script. Commented Sep 20, 2016 at 2:17
  • stackoverflow.com/a/38576759/5334188 check this, this might help Commented Sep 20, 2016 at 2:51

1 Answer 1

4

Here is how you can check if pycurl is installed:

# if you want to now install pycurl and run it, it is much trickier 
# technically, you might want to check if pip is installed as well
import sys
import pip

def install(package):
    pip.main(['install', package])

try:
    import pycurl
except ImportError:
    print 'pycurl is not installed, installing it now!'
    install('pycurl')

# not recommended because http://stackoverflow.com/questions/7271082/how-to-reload-a-modules-function-in-python
import pycurl
. . . .
# better option:
print 'just installed pycurl, please rerun this script at your convenience'
sys.exit(1)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you 2ps. Exactly what i need.
This will not work if the way that the pycurl package gets installed relies on a .pth file being updated/created in the site-packages directory. This is because the directory specified by the .pth file would normally be added to sys.path on Python startup. You would still be missing that path to find the module if it was installed in a way that required .pth file. So it may work, it may not. Really depends on how the module gets installed.
thanks buddy, surprised this doesn't have more upvotes. I assume this is still considered best practice?
I can't say if it's best practice, but it is certainly something that I have used in production.

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.