Is there any way to have a python program that tells the user it needs a module to run and then the program can install it for the user
-
is the user likely to have pip or easy_install installed?Rahul Gupta-Iwasaki– Rahul Gupta-Iwasaki2012-08-22 23:32:22 +00:00Commented Aug 22, 2012 at 23:32
-
While this is possible it doesn't mean it is a good idea to install stuff automatically. Such solutions would have a great potential for spreading malware.Roland Smith– Roland Smith2012-08-22 23:33:34 +00:00Commented Aug 22, 2012 at 23:33
-
The user wouldn't have pip or easy_installer installed and the program would ask for permission before installing.gevvek– gevvek2012-08-22 23:36:00 +00:00Commented Aug 22, 2012 at 23:36
-
If your program needs a module that is very unlikely to be found on the target system, you could add it to your distribution. Provided that the license of the module in question allows it, of course.Roland Smith– Roland Smith2012-08-22 23:59:39 +00:00Commented Aug 22, 2012 at 23:59
5 Answers
You can catch the ImportError exception to tell the user the program needs a certain module;
try:
import numpy as np
except ImportError:
print "This program requires numpy!"
print "Please download it from http://numpy.scipy.org/ and install it"
print "before running this program."
exit(1)
Note that this is just a way to exit the program in a nicer manner without a backtrace that is possibly unclear for a user. If the module cannot be found, the exception will be raised anyway.
You should only do this for modules that are not part of the python standard library.
Trying to automatically install it is not a good idea. First because it is a security risk (great way of spreading malware) and second because different operating systems or distributions have different methods of handling and registering software installation.
2 Comments
virtualenv? These questions and more make this complicated. For installation in the system-wide python directory, you'll probably need root privileges. Besides, as a sysadmin I certainly would not like it very much if software tries to install other stuff without my knowledge and conscent and separate from whatever mechanism I use to keep all installed software up to date.This doesn't answer your question exactly, but I think it might be the answer to what you want to do. I would recommend not editing the users' installed modules if possible, if you just need them in order to run a script then you should really consider PyInstaller, which bundles your program into a .exe file.
If you are making a setup script in order to configure someone's machine, this obviously won't work but otherwise it may be a good idea.
Comments
You can use
import os
os.system('[sudo] [install whatever]')
to install whichever modules you need... It will prompt the user for the admin password if it doesn't already have admin privs
http://forums.devshed.com/python-programming-11/running-bash-command-110687.html
EDIT:
Per Matthew Trevor's suggestion below, you could also try adding your dependencies to setup.py as described in this article http://www.siafoo.net/article/77
6 Comments
setuptools, pip and easy_install will install a package without registering it with the package management system. In the long run this is a nuisance for the sysadmin because he now has to keep two sets of packages up to date. And maybe ruby gems, perl modules et cetera...setup.py and let the existing distribution tools work for you.