Question

I'm trying to run a python script from a python program by kicking it off from subprocess (The reason is that the main program has to have exited when the script runs, with a combination of wx.CallAfter and Close). However when the script runs I get an error on line 1 with ImportError: No module named os which makes me think it's something to do with the PythonPath, but I can run the script just fine from a terminal.

Why can't the script see any core modules when run this way?

Edit: The line in question is:

wx.CallAfter(subprocess.Popen,'python %s "%s" %s %s'%(os.path.join(BASE_DIR,"updatecopy.py"),BASE_DIR,pos[0],pos[1]),shell=True)

BASE_DIR is just the directory that the script lives in.

Was it helpful?

Solution

subprocess is there because os.exec* has been deprecated so I wouldn't suggest using that in place of Popen as someone suggested.

I've seen this issue crop up when running from a frozen process. If that is the case then you're most likely inheriting a weird environment for the new python process.

Most frozen scripts will be trying to run from a zip file, in which case it's no wonder that Python can't find anything, it's all trapped in a zip file :)

If this is the situation then try running using the python executable that you are using to run the frozen script. It should be able to deal with the special environment.

OTHER TIPS

Maybe you could use os.execv instead of Popen.

From os/python docs:

These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process id as the caller. Errors will be reported as OSError exceptions.

(emphasis mine)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top