run multiple python scripts at the same time
Asked Answered
C

9

11

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.

Clinch answered 22/8, 2018 at 14:55 Comment(1)
It is not related to vsc or spyder but hope this might give you some thought. People use Python for everything, from short scripts to running Instagram. πŸ˜ƒ . If you want to execute multiple files at once you could consider a shell script or using something like subprocess in Python's stdlib to run multiple files simultaneously. – Smash
A
13

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.

Amandaamandi answered 22/8, 2018 at 19:22 Comment(1)
"at the same time" please read carefully the cuestion – Substitutive
N
6

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,...]).
Niagara answered 22/8, 2018 at 15:29 Comment(0)
E
2

You only need Ctrl + Shift + `

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

Elimination answered 8/5, 2021 at 4:1 Comment(2)
"at the same time" please read carefully the cuestion – Substitutive
At the same time, you can run using this method, I run always in vscode. – Tiein
V
2

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

Vitalism answered 14/12, 2021 at 8:5 Comment(1)
This extension is very poor with options (like ctrl-c it for example), it only runs simple programs that stop by themselves. – Daumier
P
2

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,
            "cwd": "${fileDirname}",
            "suppressMultipleSessionWarning": 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.
  • You can run the same file or different files simultaneously that way. If the option suppressMultipleSessionWarning is not set or set to false, you will see a warning.
  • 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.

Portemonnaie answered 9/12, 2022 at 17:24 Comment(0)
S
2

In VS code GUI you can click on "Run Python File in Dedicated Terminal" as well. It's pretty easy and straightforward.

enter image description here

Subjunction answered 29/2 at 4:29 Comment(0)
A
-1

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

Acuate answered 16/7, 2021 at 19:46 Comment(0)
N
-3

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.

Number answered 27/11, 2019 at 9:16 Comment(0)
F
-3

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

Firetrap answered 8/5, 2021 at 3:11 Comment(1)
"at the same time" please read carefully the cuestion and the program is VSCode – Substitutive

© 2022 - 2024 β€” McMap. All rights reserved.