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:
python -v wrapper stops being verbose on re-exec);sys.executable being 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.
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 can execfile() 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 of imp.load_module(). Although control will be returned to the executive script, one big advantage is that, quoting the docs:
It is different from the import statement in that it does not use the module administration — it reads the file unconditionally and does not create a new module.
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.executableIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With