1

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

4
  • is the user likely to have pip or easy_install installed? Commented 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. Commented 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. Commented 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. Commented Aug 22, 2012 at 23:59

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you think you could show me how to install with permission? This program only needs to run on linux.
Installation can be very complicated. Which package manager do you want to use, if any? For which distribution of linux? Are you using a 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.
0

You could create a function that tries to load a specific module and if it is unable to do so then provide directions for the user to download manually or you can script to download the module automatically.

Comments

0

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

0

Its easy, but since not all users would have python or pip installed in their system, it wouldn't work with every system. The code is-

import os
os.system('<command here>')
os.system('pip install numpy')

It would work...

Comments

-1

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

The details of how to install additional software vary a lot between operating systems. Plus, you'll probably need superuser (administrator) priviliges to install things globally.
hmm, do pip/easy_install cover most of the variations in installation requirements between systems, b/c then @gevvek could prompt the user to install that and then use those to install whatever other modules
Yes and no. If the OS uses a package management system (like most Linux distributions do), then 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...
interesting, I guess how useful this solution is depends then on how polished of a program gevvek is looking for then =P
Hard coding OS-level installation commands inside an application is a terrible solution no matter what. The point for ensuring dependencies are met is before the program actually runs. Don't waste your time reimplementing aspects of setuptools inside of your code, just write a setup.py and let the existing distribution tools work for you.
|

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.