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 : )