-1

So I was hoping to write my code more resiliently, starting from the top in python I was thinking of addressing importing.

I want code to run on systems where required packages haven't been installed. To achieve this I was hoping to install packages on the run with python.

try:
    import pygame as pg
except(ImportError):
    # [install pygame][1] here
    # Download and run pygame.MSI (windows)
    # apt-get install python-pygame

install pygame From this specific solution I intend to make a more generic function ...

import subprocess as sp
def imp(inP,name,location):
    try:
        exec "import "+inP+" as "+(name if name != "" else "")
    except ImportError:
        try:
            os = ????
            if(os == windows):
                sp.call("pip install "+location,shell=True)
            if(os == unix):
                sp.call("sudo apt-get install python-"+inP,shell=True)
            r = True
        except Exception:
            print colPrt("ERROR installing ") + inP
            r = False
        try:
            exec "import "+inP+" as "+(name if name != "" else "")
        except(ImportError):
            print colPrt("ERROR importing ") + inP
            r = False
    return r

and so my one question becomes 2. The first being the best practicing for installing modules on the run and the second being how that differs between a unix and windows environment.

Ps, colPrt simply returns red text to the terminal

def colPrt(s):
    return("\x1B["+"31;40m" + str(s) + "\x1B[" + "0m")

thanks for your thoughts : )

Community
  • 1
  • 1
kpie
  • 9,588
  • 5
  • 28
  • 50
  • 2
    FWIW, I believe that `except (ImportError e)` is not proper syntax . . . – mgilson Jan 19 '16 at 04:14
  • Consider using [python wheels](http://pythonwheels.com) to install your software. If you put dependencies in the wheel, they will be installed when your software is installed. The nice thing about it is that the user will do any privilege raising needed for install for you. – tdelaney Jan 19 '16 at 05:28

3 Answers3

3

The first being the best practicing for installing modules on the run

I would advise against installing modules on the run for a couple reasons:

  1. Users should have a choice about whether they want to install stuff. Most users aren't interested in making that choice at runtime (and your program shouldn't be running with the necessary privileges to install stuff like that anyway)...
  2. Follow up to the first point -- Dependencies should be installed when your package gets installed.
  3. It'll clutter your code with a bunch of stuff that you really don't want to maintain.
  4. Even if you do include it, whose to say that your code and the latest version of some third-party package on the web are compatible?

Python's standard build system has ways of interacting with dependencies, so when your package gets downloaded and installed (e.g. via easy_install or pip), then the dependencies should all come with it.

Take a look here for some advice on how to package your python code.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • While we're at it, if you *are* going to do this goofy shit, you should at least use [`importlib.import_module()`](https://docs.python.org/2/library/importlib.html#importlib.import_module) instead of `exec`. – Kevin Jan 19 '16 at 04:09
0

Try to look at the pip-accel, it is wrapper over pip, that is working cross platform. If you users have no python environment pre installed, I want recommend you to look at virtualenv. Pip + virtualenv works like a charm, you just have to write short python script that will run it with setups.

Anyway, If you will use your own solutions, it will be very difficult to support, maybe one day your boss come and ask you to add Mac Os support. Better to write you wrapper over pip+virtualenv than implement all logic by yourself.

fedorshishi
  • 1,467
  • 1
  • 18
  • 34
0

For exception write out only error message and let the choice of installing modules on user. Or write it ower setup (install) wrapper.

On unix / linux platforms the python is distributed with easy_install and do not try nothing to install in background with apt-get. Without permissions would not succeed. Needless.

And we used not only Debian based linux :) eg. my standard OS is unix based: QNX :)

vatay
  • 387
  • 2
  • 9