Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-execute Python interpreter with a different script effectively

Tags:

python

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:

  1. Any options passed to the Python interpreter are not preserved (e.g. python -v wrapper stops being verbose on re-exec);
  2. The Python interpreter is re-executed unnecessarily (with PyPy it adds 0,7s on my system);
  3. It relies on sys.executable being useful, and the docs say that:

    If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None.

I'm wondering what replacement I should use for the os.execl call to solve all the issues. So far, I can tell that:
  1. 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;
  2. 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+.

like image 359
Michał Górny Avatar asked Feb 06 '26 13:02

Michał Górny


1 Answers

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:

  1. interpreter options will be preserved
  2. interpreter will not be re-executed unnecessarily
  3. it does not rely on the value of sys.executable
like image 171
martineau Avatar answered Feb 08 '26 03:02

martineau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!