Python Forum
re-executing self with different arguments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
re-executing self with different arguments
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Try
import sys
subprocess.Popen([sys.executable, sys.argv[0]]+newargs)
Reply
#3
sys.executable was what i needed.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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())
Reply
#6
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 3,881 Mar-02-2017, 10:23 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020