Here is the extension for python I used in the vs code: python extension.
When I use the debugging feature provided by the extension, it will hang in there and do nothing if it needs input from the command line.
Where can I input values to step over the input statement in the vs code?
The externalconsole directive is deprecated. Use console instead and indicate your preference for external this way:
"console": "externalTerminal"
The application output (and input) will go to a separate window so the VS Code debug console remains a pure python prompt where you can evaluate stuff during breakpoints.
The trick to getting this to work is on the extension's(Don Jayamanne's Python) wiki page. You have to include "externalConsole": true setting in your launch.json file's "name": "Python" section.
The extension's wiki confirms that this does not work by default:
This allows for capturing of input from the console/terminal window
applications, which isn't possible in the standard VSCode debugger.
Here are the steps to getting this to work:
From the Debug window (Ctrl+Shift+D), press the little gear icon to open (or to generate) a launch.json file. It gets placed into a .vscode directory in what ever folder you have selected as your "Open Folder" in VS Code.
You have to add pythonPath parameter to the first configuration block. This is needed to have the debugger work at all.
You also have to add and externalConsole parameter to the same block. This is what is needed to have the debugger accept input. When you debug, a separate window will open outside of VS Code but works well otherwise.
After you add both settings, the block should look something like this. I did not have to change anything else in the rest of the launch.json file.
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${file}",
"pythonPath": "C:/Users/igor/Documents/Tools/WinPython-32bit-3.4.3.7Slim/python-3.4.3/python.exe",
"externalConsole": true,
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
The console option could have any of these values: internalConsole, integratedTerminal, externalTerminal.
Normally if you start the debugger and the program stops, it leaves the external terminal displaying a prompt Press Enter to continue . . . to have access to any output of the program. If you accidentally have a syntax error the external terminal just closes not leaving any message.
When using the integratedTerminal option, the terminal stays there and displays the error message.
While I do not know if this externalTerminal thing is a bug or not, the integratedTerminal option seems to work much better in this case.
VS Code has an option for you to Debug with Python console.
You just hit Ctrl + Shift + D and beside the blue play icon, click the down arrow, and choose Python Console App instead of just Python, like this:
Updated Information on Console Options
Most of the higher-rated, original answers are no longer valid, or unclear exactly what to set and how. See below for details how to set the "Console" option in launch.json, and what all the options are.
You can choose either an internal or external terminal setting and have keyboard input work during debug. One option for output, the Debug Console, does not currently (Fall 2019) allow keyboard input to your program, although you can always use the debug console to enter live debug and code commands.
The steps to set the available options are below.
Opening the Debug Configuration launch.json file
Click the debug icon (update early 2020 - now the "Run" icon): to open the debug sidebar (again, now called the "Run" sidebar, and the command menu name is also changed from debug to run).
At the top of the screen, ensure "Python: Current File" is selected. You may need to select it or create it (might need to create your first debug/run configuration):
Click the gear icon to the right of the configuration dropdown selected in prior step. This will bring up the launch.json for that configuration in the editor.
Update the "console": option to one of the settings described below
Valid "console" settings in launch.json
"console": "internalConsole"
this is the default setting
uses the internal debug console
as of 10/2019 does not allow keyboard input.
"console": "integratedTerminal"
this spawns a new Python Debug Console terminal window every time you debug (I wish it would reuse any existing one, but it doesn't - use the trash can symbol on the upper right of the terminal window to remove old, unused terminals)
The type of terminal created is based on the default terminal type you setup (i.e. command window, bash shell, etc.).
All standard output will be in this terminal and you can enter keyboard input if the program is waiting for it.
You can switch to the DEBUG CONSOLE tab if you want to run command during debugging.
"console": "externalTerminal"
this spawns a separate terminal outside of the VS Code process as the terminal for your code to run in on run or debug.
the external terminal will be the default type for your OS (command window for Windows 10).
this terminal is separate from VS Code and will normally add a Press any key to continue... prompt after your program terminates so that you can view/copy any output before it disappears.
All standard output will go to that terminal and keyboard input can be entered in it.
You can switch to the DEBUG CONSOLE in VS Code when the code is paused to enter debug commands.
In Visual Studio Code click the pick list to the right of the green arrow. Then select Python: Terminal (external). When you launch your script it will run in an external window and allow you to key in input.
Change the launch.json and put this into your java code
{
"type": "java",
"name": "Debug (Launch)",
"request": "launch",
"cwd": "${workspaceFolder}",
"console": "externalTerminal",
"stopOnEntry": false,
"mainClass": "",
"args": ""
}
simply:- step1. click on small gear-icon of debugger window. step2. make "true" to this ["externalConsole": false,] in launch.json file.
step3. and just restart your debugger.
Related
I want to debug a python module in vscode to save time and help me figure out what's going on with the code. But I'm having two problems (I'll focus more on the 1st issue for this post) that seem like they're related to me not using launch.json correctly and I would like to know what's going wrong. I'm working on a Mac.
My module is called __main__. The full absolute path to it would be ~/Code/APPRES-483/cqs/update-query-definition/__main__.py (__main__.py is a script, but it seems the module is called __main__ as well).
There's a virtualenv at ~/Code/APPRES-483/venv-cqs/ that I'm using. I opened up a VSCode window inside of ~/Code/APPRES-483/cqs/update-query-definition/.
Right now my launch.json file looks like this.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
// "python":"~/Code/APPRES-483/venv-cqs/bin/python",
"type": "python",
"request": "launch",
"module": "__main__",
"pythonArgs":[
"-v",
"--file-to-write", "~/Code/APPRES-483/LOCAL-query-definition.yml"
],
},
]
}
The first problem is that I can't actually set the python interpreter path in launch.json. I have my virtualenv active inside of VSCode (i.e. I see the prefix for venv-cqs that I intend to see).
If I open up a terminal and put ~/Code/APPRES-483/venv-cqs/bin/python, that correctly opens up the python terminal/executable inside of venv-cqs/bin. If I add a line like "python":"~/Code/APPRES-483/venv-cqs/bin/python", I get the message saying "the python path in your debug configuration is invalid."
I tried to follow https://stackoverflow.com/a/66885651/6432861 instructions to use the Python: Select Interpreter command. I'm getting some weird behavior.
If I try to browse finder/files to find the python executable in venv-cqs/bin/, once I double click on the file it doesn't actually get made into the python interpreter for vs code. If I hover my mouse in the bottom left, I'm still seeing the address of my default python installation in /usr/local/bin or wherever it is.
But if I copy and paste ~/Code/APPRES-483/venv-cqs/bin/python rather than trying to browse for files, that successfully changes the python interpreter and I can see that at the bottom left of my screen.
The only way that the code avoids these errors is if I don't have a python field in the configuration for launch.json. I know that's not how it's supposed to be so... I want to know what's going wrong.
The second problem I'm having is with pythonArgs. But I'll try to make another post about it since this is already long enough.
First, you may set
"python.defaultInterpreterPath": "~/Code/APPRES-483/venv-cqs/bin/python",
in User Settings.json to specify python interpreter. If it doesn't work and VS Code still shows default python installation as python interpreter in bottom-left corner, deleting the user data folders $HOME/Library/Application Support/Code and .~/.vscode can reset VS Code.
Second, the setting python defaults to the interpreter selected for your workspace, so once you have selected the venv-cqs as python interpreter, debug would use it by default and if you insist that, set
"python": "${command:python.interpreterPath}"
This is running Visual Studio Code (version 1.55.0 user setup) on Windows 10, with python 3.9.
Whenever I try to add breakpoints to my python files, it marks it in the GUI as a breakpoint (red circle), but when I try to debug it goes right over them as if I never included them at all.
As a test I created an empty folder with only a short python test file, and the debugger went right over the breakpoints.
For context here is the brief test file (I made every line a breakpoint):
print('test')
print('test2')
foo = 1+2
print(foo)
I tried initially to use the default debug configuration (by clicking run -> start debugging -> python file). When that didn't work I thought that maybe the default was having an issue, but even manually creating the json file did not fix it.
Json file for context:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
This seems similar to a question posed here: Debugger Not Stopping at Breakpoints in VS Code for Python however, either my issue is caused by something else, or that information is outdated, because neither the solution of adding "justMyCode": false to the json file; nor just re-installing everything, fix anything.
A newbie reason why you are not stopping on breakpoints (especially if you are not used to VSCode):
If you run the code using the green arrow icon, the breakpoints will not be hit. You will be able to set breakpoints and see the red dot next to the line, but the debugger will not stop on any of them.
If you start execution with F5, then the debugger will stop on the breakpoint(s).
The green arrow icon is "Run Python in terminal" which ignores breakpoints, while F5 is "Continue" which invokes the debugger.
Are you using python 3.9.3? I had the same issue, but it is related to python version and was fixed on 3.9.4.
Source: https://github.com/microsoft/vscode-python/issues/15865
I am brand new to VSCode with Python. Is it possible to write to the tab DEBUG CONSOLE (only) from Python code? I have Googled but found nothing on this.
The debugger does this internally, e.g. on a breakpoint set to Log Message. For the moment, if I use print() that does go to DEBUG CONSOLE, but it also goes to TERMINAL tab, which I'd rather it did not. The launch.json has "console": "integratedTerminal" if that is relevant.
You can set "console": "internalConsole" to have output go to the debug console.
When executing "Run Selection/Line in Python Terminal" command in VSCode, terminal's current working directory is the workspace root directory. How can we set current directory of terminal to the current file's directory when running the selection/line?
In "User Settings", use the search bar to look for "python.terminal.executeInFileDir" and set (=) its value to "true" instead of "false".
Update following release 2019.10.44104
Following release 2019.10.44104 of the VS Code python extension, you can now set the python.dataScience.notebookFileRoot to ${fileDirname} to directly start the python interactive window in the directory of the file you're running.
Note that the root directory will not change if you then run code from another file unless you interrupt/restart the kernel (or close VS Code). On this aspect, see the following comment and the corresponding github issue.
For the Python Interactive Window, the setting you're looking for is python.dataScience.notebookFileRoot. However, as explained in this answer to a similar question,
Always opening on the file location (without having to set notebookFileRoot to an absolute path per folder) is not supported via the notebookFileRoot setting. The VSCode variables such as ${fileDirname} are specific to task and debug configuration files (launch.json and task.json).
See also the associated github issue.
As indicated, you can still set this setting to a specific absolute path, which might be enough if you're mainly working on a single project at a time.
Alternatively, you could also add the following code at the top of your script/notebook:
import os
os.chdir('absolute-path-to-workingDir')
I used the option Run -> Add Configuration (or Open configuration, if available)
This will open your current 'launch.json' file.
Now you may add this line to the configuration wanted (in my case was Python):
"cwd": "${fileDirname}"
This line will make VSCode to run your stuff in the same folder as the file is being executed.
You can get more details in this link:
https://code.visualstudio.com/docs/editor/variables-reference
Here is my full json file (just for reference):
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
}
]
}
you need to go to file/preferences/user settings and click the "{}" icon at the top right of the window. After that, put this setting in: "terminal.integrated.cwd": "C:\\Users\\myUser\\", and after that wherever your terminal's directory happens to be. This answer is not the most inaccurate cause im still a noob myself at using vscode so if someone more experienced with it could reply to this thread it would be great.
There is no straightforward way to achieve this yet. In search for a better solution, I have a workaround with the Terminal Here extension in the VScode Marketplace. This extension allows you to open an integrated terminal in the current file's directory. This extension combined with a few more steps and you should get the desired behavior.
Once the extension is installed, make sure your file window is in focus, and press ctrl+shift+p and execute Terminal Here: Create Terminal. This will create a new terminal in the file's directory.
Type python in the terminal to launch the Python interpreter.
Now, position the cursor on the line you wish to execute and press ctrl+shift+p and execute Terminal: Run selected text in active terminal. This will run that line of code in the open python interpreter.
The first two steps are required only for the first time you run a code selection in the Python interpreter in the current file's directory. All subsequent selections can be run with the third step. To make things quicker, you could attach custom keybindings to the first and last steps.
This options will help you.
File->Preferences->Settings.
Add or edit the below setting.
terminal.integrated.shell.windows": ""
From the next terminal it will be reflected.
And add .profile to your default shell and add default path to it.
More information at: https://code.visualstudio.com/docs/editor/integrated-terminal
this issue is answered here. The solution is to config the launch.json file in .vscode folder
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 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.
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