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.
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.
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,...]).
You only need Ctrl + Shift + `
It will create a new terminal and you can run another script.
There is an extension called "Code Runner" extension developped by Jun Han, after install it, right click on the second script, select "Run Code".
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 tofalse
, 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.
In VS code GUI you can click on "Run Python File in Dedicated Terminal" as well. It's pretty easy and straightforward.
open a new angle of the visual studio then open the other file in this new one, so you can run it
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.
Use Sublime Text 3 and run your script by Ctrl + B shortcut
© 2022 - 2024 β McMap. All rights reserved.