I want to check if a module exists, if it doesn’t I want to install it.
How should I do this?
So far I have this code which correctly prints f if the module doesn’t exist.
try:
import keyring
except ImportError:
print 'f'
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is how it should be done, and if I am wrong, please correct me. However, Noufal seems to confirm it in another answer to this question, so I guess it’s right.
When writing the
setup.pyscript for some scripts I wrote, I was dependent on the package manager of my distribution to install the required library for me.So, in my
setup.pyfile, I did this:So if
package_namewas installed, fine, continue. Else, install it via the package manager which I called usingsubprocess.