cmd='python -S -c "import random; print random.randint(0,10)"'
Goebel Consult
http://www.goebel-consult.de
Monatliche Kolumne:
http://www.cissp-gefluester.de/2010-01-hinterturen-allen-ortes
Blog:
http://www.goebel-consult.de/blog/200505010
Goebel Consult ist Mitglied bei http://www.7-it.de/
Help please! How am I being dumb! grin!This example python script runs python via Popen. It simply tries to print a random number. But the PYTHONPATH is redefined without the system's python <lib thus it is unable to find the random module.Bonus: I assume once I can get past this, then I will not longer need the -S as the builtin site module will be found.PROGRAM:import subprocessimport shlexdef run_cmd(cmd):cmd=shlex.split(cmd)try:output=subprocess.check_output(cmd, stderr=subprocess.STDOUT)except Exception as exc:print ">>>", exc.outputelse:print outputdef main():cmd='python -S -c "import sys; print sys.path"'run_cmd(cmd)print "\n\n"
cmd='python -S -c "import random; print random.randint(0,10)"'
run_cmd(cmd)if __name__=="__main__":main()SPEC:# -*- mode: python -*-a = Analysis(['test.py'],pathex=['C:\\Users\\???\\Desktop\\testing'],hiddenimports=[],hookspath=None)pyz = PYZ(a.pure)exe = EXE(pyz,a.scripts,a.binaries,a.zipfiles,a.datas,name=os.path.join('dist', 'test.exe'),debug=False,strip=None,upx=True,console=True )EXECUTION:C:\Users\???\Desktop\testing>dist\test.exe['', 'C:/Users/???/AppData/Local/Temp/_MEI92722', 'C:/Users/???/Desktop/testing/dist', 'C:\\Windows\\system32\\python27.zip', 'C:/Users/???/AppData/Local/Temp/_MEI92722/\\DLLs', 'C:/Users/???/AppData/Local/Temp/_MEI92722/\\lib', 'C:/Users/???/AppData/Local/Temp/_MEI92722/\\lib\\plat-win', 'C:/Users/???/AppData/Local/Temp/_MEI92722/\\lib\\lib-tk', 'C:\\PYTHON27']>>> Traceback (most recent call last):File "<string>", line 1, in <module>ImportError: No module named random