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
jebjeb
  • 115
  • 1
  • 4
  • 12
  • 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. – Brian Cain Sep 20 '16 at 01: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`? – Paul Rooney Sep 20 '16 at 01: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. – jebjeb Sep 20 '16 at 02: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. – jebjeb Sep 20 '16 at 02:17
  • http://stackoverflow.com/a/38576759/5334188 check this, this might help – be_good_do_good Sep 20 '16 at 02:51

1 Answers1

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)
2ps
  • 15,099
  • 2
  • 27
  • 47
  • Thank you 2ps. Exactly what i need. – jebjeb Sep 20 '16 at 03:07
  • 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. – Graham Dumpleton Sep 20 '16 at 03:15
  • thanks buddy, surprised this doesn't have more upvotes. I assume this is still considered best practice? – Umar.H Dec 26 '19 at 21:02
  • I can't say if it's **best** practice, but it is certainly something that I have used in production. – 2ps Dec 27 '19 at 01:53