Сергей
Сергей

Reputation: 3

Running two scripts using the subprocess library in VSCode

I have two independent scripts that need to be run all the time and display messages in the terminal: script1.py and script2

To run the scripts I use the code editor VSCode under Windows. In order to use the 2 independent terminals I create them separately and in each of them I write the command: python script1.py and python script2.py

Each time I restart, I have to do it all over again. (yes it's not hard, but I think there is a way that could run both scripts and create separate terminals in VSCode)

What I tried to do:

import subprocess

subprocess.Popen(["python", "script1.py"], creationflags=subprocess.CREATE_NEW_CONSOLE)
subprocess.Popen(["python", "script2.py"], creationflags=subprocess.CREATE_NEW_CONSOLE)

This execution option brings up two separate command-line interpreter windows. This is the usual сmd.exe so if I didn't use VSCode but ran the scripts directly through the Windows console.

import subprocess

p1 = subprocess.Popen(["python", "script1.py"], shell=True)
p2 = subprocess.Popen(["python", "script2.py"], shell=True)

This execution option outputs the result of 2 scripts into one VSCode terminal, which is inconvenient.

Upvotes: 0

Views: 54

Answers (1)

MingJie-MSFT
MingJie-MSFT

Reputation: 9427

In fact, the solution to the problem itself has been written in your question:

In order to use the 2 independent terminals I create them separately and in each of them I write the command: python script1.py and python script2.py

This is the most correct and safe method. Of course, if you feel troublesome, I can provide the following alternatives:

  1. Install code-runner extension, then use Run Code button (default shortcuts "Ctrl+Alt+N") to run the python1.py then Run Python file (default shortcuts "Shift+Enter") button to run the python2.py.

enter image description here

  1. Use jupyter-notebook, and create the same codes in python1.ipynb and python2.ipynb. Run the codes in interactive windows is a good choice aswell.

Upvotes: 0

Related Questions