Python Forum
re-executing self with different arguments - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: re-executing self with different arguments (/thread-20556.html)



re-executing self with different arguments - Skaperen - Aug-18-2019

i want to have a script re-execute itself (a few instances of itself in child processes) with different arguments. the problem i am running into is that sys.argv[0] is not always the first component of the command. if the script is not directly executable and must be run by executing the python3 engine itself with the file name of the script as the first argument, then something like subprocess.Popen([sysargv[0]]+newargs) won't execute (another instance of) the python3 engine. i don't even know where the script can find out if it was executed this way and of so, where to find how it was executed (the real C-level argv[0][] value the first instance of the engine started with).

does anyone know what needs to be done for this?


RE: re-executing self with different arguments - Gribouillis - Aug-18-2019

Try
import sys
subprocess.Popen([sys.executable, sys.argv[0]]+newargs)



RE: re-executing self with different arguments - Skaperen - Aug-19-2019

sys.executable was what i needed.


RE: re-executing self with different arguments - Skaperen - Aug-28-2019

it turns out that sys.executable just always gives the engine path whether it was used in the command or not. there are 2 ways to run a Python script on POSIX. assuming the script is named "foo.py" and has been made executable with "chmod 755 foo.py". you can do "python3 foo.py" or "./foo.py". if "." is in the path list in the PATH environment variable then simply typing "foo.py" will also run it. what i am wanting is to determine whether the script was run directly or if the name or path to the engine was used. this could be difficult because hash-bang execution will still be running the engine and giving it the file path as the first argument and the typed arguments following that. so C level argv[] will look the same, either way. this is so i can re-run the script exactly the same way in a child process (i'm changing the arguments).


RE: re-executing self with different arguments - Gribouillis - Aug-28-2019

Skaperen Wrote:what i am wanting is to determine whether the script was run directly or if the name or path to the engine was used.
I don't think you can do that from inside the script. On the other hand you can get the pid of the current process and probably use a module such as psutil to find the command line associated to this process if any.
import psutil
print(psutil.Process().cmdline())



RE: re-executing self with different arguments - Skaperen - Aug-28-2019

i never needed this in C, but i did read that there was a subtle way. it had to be done right near the top of the C code, so maybe the info is lost unless the interpreter has it coded.