I’m working on a wrapper script written in Python. The wrapper is supposed to choose another Python script based on the system state and execute it (using the absolute path). There is no need to return to the parent script.
It should be noted that I have no control over the scripts being run. They can use __name__ checks, access sys.argv and it should all behave like if the script was run directly.
Right now, I’m using os.execl():
import os, sys
# ...
os.execl(sys.executable, sys.executable, new_script, *sys.argv[1:])
But I can count at least three issues with that:
- Any options passed to the Python interpreter are not preserved (e.g.
python -v wrapperstops being verbose on re-exec); - The Python interpreter is re-executed unnecessarily (with PyPy it adds 0,7s on my system);
- It relies on
sys.executablebeing useful, and the docs say that:If Python is unable to retrieve the real path to its executable,
sys.executablewill be an empty string orNone.
I’m wondering what replacement I should use for the os.execl call to solve all the issues. So far, I can tell that:
execfile()would probably work but it was removed in Python3 and re-implementing it by hand AFAICS is ugly (because of the encoding issues). I’m not sure what other implications canexecfile()have;imp.load_module()would probably work but it’s a bit hacky and was deprecated in Python3.3. It probably can suffer Python3 encoding issues as well.
Which solution do you suggest I use?
Edit: I’d forget. The solution has to work with Python 2.5+, PyPy and Jython 2.5+.
I would just use
execfile()instead ofimp.load_module(). Although control will be returned to the executive script, one big advantage is that, quoting the docs:This means the script file can be anywhere, can have any (or no) file extension, and resources aren’t wasted doing module-import related tasks.
Doing so automatically accomplishes or avoids the things you desire:
sys.executable