1

I'm computing neural networks with several .py files in spyder. I could sequencely execute them in spyder. However, the console do not show any thing like single file execution. For example, this is the single file ouput:

In [1]: runfile('NN.py', wdir='D:/test')
[1/200] losses:... loss_D:... loss_G:...
...

This is the sequence file:

[1]runfile('D:Sequential_execution_test.py', wdir='D:/test')

There is only blank in console of spyder.

Here are the sequence codes:

import os
import sys
import subprocess
from subprocess import Popen, PIPE, CalledProcessError

script_paths = [r'D:\code1.py', r'D:\code2.py']

for script in script_paths:
    os.chdir(os.path.dirname(script))
    with Popen(["python", script], stdout=PIPE, bufsize=1, universal_newlines=True) as p:
        for line in p.stdout:
            print(line, end='') # process line here
            sys.stdout.flush()

By the way, the code could work well. Only the output is not shown in console of spyder. Is there any way to shown subprocess output in spyder console? Thanks for any help. PS: I have uncheked "Hide command line output windows generated by the subprocess module." in preference.

1 Answer 1

0

Use subprocess.run() this works for me:

import os
import sys
import subprocess

script_paths = [r'D:\code1.py', r'D:\code2.py']

for script in script_paths:
    os.chdir(os.path.dirname(script))
    subprocess.run(["python", script], stdout=sys.stdout, stderr=sys.stderr)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.