run multiple python scripts at the same time

Question:

Is there a way to run multiple python scripts simultaneously in vsc. I mean while there is already a script running, I would like to run another script. When I try I get “code is already running”.
In spyder-ide I simply open a new IPython console and run the new script in this newly opened console.

Asked By: Khalil Al Hooti

||

Answers:

If you need to coordinate execution and communicate between these programs, you’ll need to use threading. If the scripts can run independently, you can run them manually at the same time from a terminal, or use a subprocess call from the first script:

subprocess.call(['python', 'secondscript.py', secondscript_arg1, secondscript_val1,...]).
Answered By: J. Blackadar

You can always open a terminal terminal window — with either Python: Create Terminal or Open New Terminal — and launch the script(s) manually in separate terminals.

Answered By: Brett Cannon

You could install PyCharm which has a plugin called ‘Multirun’.This allows you to run several python files in parallel. I had the same issue as you and fixed it this way.

Answered By: Kishan Vedia

Use Sublime Text 3 and run your script by Ctrl + B shortcut

Answered By: Ali

You only need Ctrl + Shift + `

It will create a new terminal and you can run another script.

Answered By: Sudo Band

open a new angle of the visual studio then open the other file in this new one, so you can run it

Answered By: Mohammed El Qadmiry

There is an extension called "Code Runner" extension developped by Jun Han, after install it, right click on the second script, select "Run Code".

Answered By: Wade Wang

Brief answer:

Create a debug configuration and run the script with Ctrl + F5. A button for this can be configured.

Elaborate answer:

There are multiple ways to run a Python file in VS Code. There is the triangular "Run" button (typically in the top right corner of the window) and there is the triangular "Run" button in the "Run and debug" view in the primary side panel. Use the latter one.

  • If there is no debug configuration yet, open the menu next to the run button and click on "add configuration ([your source folder])".
  • The launch.jason file should open.
  • Add a configuration like the one below (there can be multiple configurations, and you can adjust them to your liking):
"configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
]
  • Go to your source file and press Ctrl + F5. This will run your file with the configuration specified in the debug view, but the file will not be debugged.
  • If you run a second python script, you will be asked if you want to start a second instance. Simply say "yes".
  • I have not figured out how to switch off this message, but to me it is not too bothersome.
  • Because the "default" run button in the top right corner is typically not doing what I want, I disabled it (right click -> disable "run and debug").

Adding a "proper" run button to the task bar

  • If you do not like to use your keyboard, you can create a button for running. You may use the extension "Task Buttons".
  • Install the extension.
  • Add a task to your tasks.json like this:
"tasks": [
        {
            "label": "Run",
            "type": "shell",
            "command": "${command:workbench.action.debug.run}",
        }
]
  • Then add a button that executes this task (in settings.json):
"VsCodeTaskButtons.tasks": [
            {
                "label": "Run",
                "task": "Run",
                "tooltip": "Run Python file",
            }
]

Disclaimer: I did not have time to double check my answer in a fresh installation of VSCode without extensions. If the answer does not work for you, please write a comment, and I will do my best to check which extensions may be required.

Answered By: Samufi