1

I have two python scripts, foo.py and bar.py

foo.py:

#!/usr/bin/env python
os.execv('./bar.py', sys.argv)

bar.py:

#!/usr/bin/env python
print(sys.argv)

When I invoke foo.py I get the output

['./bar.py']

instead of

['./foo.py']

because the python interpreter is spawned from the shebang and sets argv[0] to bar.py, but I want to preserve the name of the original script in argv[0].

Is there a way to do that in python?

moben
  • 69
  • 8
  • 1
    Do you really need to call the separate file as a subprocess? Could you just import it, or use `execfile()`? – Daniel Roseman Nov 15 '13 at 10:28
  • 1
    possible duplicate of [How can you obtain the OS's argv\[0\] (not sys.argv\[0\]) in Python?](http://stackoverflow.com/questions/4867761/how-can-you-obtain-the-oss-argv0-not-sys-argv0-in-python) – shad0w_wa1k3r Nov 15 '13 at 10:29

1 Answers1

0

As @Daniel Roseman noted, execfile seems to do exactly what I wanted.

Sorry for apparently not doing enough research on this

moben
  • 69
  • 8