Program chaining on Windows

Rob Cliffe rob.cliffe at btinternet.com
Sun Aug 23 03:31:13 EDT 2020


On WIndows 10, running Python programs in a DOS box, I would like one 
Python program to chain to another.  I.e. the first program to be 
replaced by the second (*not* waiting for the second to finish, as with 
e.g. os.system).  This doesn't seem a lot to ask, but so far I have been 
unable to so this.  I am using Python 3.8.3.  Some attempts so far (may 
be nonsensical):

ATTEMPT #1
----------------
# File X1.py
import os
print("This is X1")
os.execl('C:\\Python38\\python.exe', 'X2.py')

# File X2.py
print("This is X2")

When I type "X1.py", it prints "This is X1", then starts the Python 
interactive interpreter.  Furthermore:
     TLDR: Weird behaviour
     Long version:  If I attempt to exit the interpreter with ctl-Z, 
this apparently succeeds (displaying the Windows command prompt), but in 
reality it is still in the interpreter, e.g. if I type "dir" it responds 
"<built-in function dir>" followed by the ">>>" interpreter prompt.  And 
this cycle (ctl-Z etc.) can be repeated ad nauseam.  If instead I try to 
exit from the interpreter with "exit()", the cursor moves to the next 
line and the interpreter waits for more input (but without displaying 
the prompt). If I try "exit()" again, the whole DOS box disappears.

ATTEMPT #2
-----------------
# File X1.py
import os
print("This is X1")
os.execl("C:\\Python38\\python.exe X2.py", '')

This raises ValueError: execv() arg 2 first element cannot be empty

ATTEMPT #3
----------------

import os, sys
print("This is X1")
os.execl("%s X2.py" % sys.executable, "X2.py")

This raises FileNotFoundError: [Errno 2] No such file or directory

ATTEMPT #4
----------------
# File X1.py
import os, sys
print("This is X1")
os.execv(sys.executable, ['X2.py'])

This behaves the same as, or similarly to, Attempt #1.

ATTEMPT #5
----------------
# File X1.py
import os
print("This is X1")
os.popen('python X2.py')

# File X2.py as previously

     TLDR: Really weird behaviour!
     Long version: If I type "X1.py", it displays "This is X1" followed 
by the DOS prompt.  If I type in a DOS command, it is executed, and the 
DOS prompt displayed.  However, if I type in another DOS command, 
nothing happens except that the cursor moves to the next line and waits 
for input (no prompt).  If I type in a further DOS command, it is 
executed.  If I type still another DOS command, I see

Exception ignored in: <io.TextIOWrapper name='<stdout>' mode='w' 
encoding='cp1252'>
OSError: [Errno 22] Invalid argument

and the cursor moves to the next line (no prompt).  If I type in one 
more DOS command, it is executed, and we appear to be back to normal DOS 
operation.

ATTEMPT #6
-----------------
# File X1.py
import subprocess, sys
print("This is X1")
subprocess.Popen('X2.py', executable=sys.executable)

This behaves the same as, or similarly to, Attempt #1.

ATTEMPT #7
-----------------
# File X1.py
import subprocess, sys
print("This is X1")
subprocess.Popen('-c X2.py', executable=sys.executable)    # added -c

# File X2.py
print("This is X2")

Some progress (maybe).  This prints "This is X1", then the DOS prompt 
followed by "This is X2", then the cursor moves to the next line and 
sits waiting for input (no prompt).  If I then type something in, this 
is interpreted as a DOS command, and finally the DOS prompt is 
displayed.  To find out more about what is happening:

ATTEMPT #8
----------------
# File X1.py as above

# File X2.py
print("This is X2")
input("Press Enter to continue X2")
input("Press Enter to quit X2")

If I type "X1.py", it displays:

This is X1
C:\Python38>This is X2
Press Enter to continue X2

Now:
     TLDR: More weird behaviour, as if Windows and X2.py were taking 
turns to collect lines from the console.
     Long version: Now if I type something in and press Enter, it is 
interpreted as a *DOS command".  Then the DOS prompt is displayed. Now 
if I (type something and) hit Enter, I see

Press Enter to quit X2

Now if I type something and hit Enter, it is interpreted as a DOS 
command, and the DOS prompt is displayed again.  Now if I type in a DOS 
command and press Enter, it is ignored but the cursor moves to the next 
line and waits for input (no prompt).  Now if I type another DOS 
command, it is executed.  Finally we appear to be done (the DOS prompt 
is displayed and we are back to normal DOS operation).


Am I missing something?  Is there a way in Windows for one Python 
program to "chain" to another (or indeed to any executable) without 
waiting for the latter to finish?
Thanks in advance
Rob Cliffe



More information about the Python-list mailing list