Visual Studio Code Linux python debugger doesn't execute - python

I'm on a Jetson Nano Linux Ubuntu 18.4, remoting from a windows platform
using VS Code to develop python code.
I'm using Visual Studio Code's debugger and expecting the code to stop at several break points.
It doesn't. I'm thinking VSC can't find the .py file from the launch.json file? I don't know how to debug launch.json.
Any thoughts?
Python code:
#!/usr/bin/python3
import sys
import argparse
print(f'Length : {len(sys.argv)}')
if len(sys.argv) == 2:
print(f'Argv[1]: {sys.argv[1]}')
if len(sys.argv) == 2:
name = sys.argv[1]
else:
name = 'stranger'
print(f'Hi there, {name}')
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Hello World",
"type": "python",
"justMyCode": true,
"request": "launch",
"program": "${workspaceFolder}/build/bin/helloWorld.py",
"args": ["Steven"],
"console": "integratedTerminal",
"cwd": "${workspaceFolder}"
}
]
}
The runs without issue from the TERMINAL window:
steven#DEVELOPMENT-JETSON:~/Projects/test-python/helloWorld/build/bin$ ./helloWorld.py steven
Length : 2
Argv[1]: steven
Hi there, steven
Attempted 3 Different run attempts in Visual Studio Code:
A. right clicked on helloWorld.py from the Explorer window>> Clicked [Run Python File in Terminal]
Application runs in TERMINAL window and errors out as expected.
Application required an argument e.g $./helloWorld.py Steven. No where to
add a parm.
B. clicked Run >> clicked Run without debugging
Nothing happens except the debug window flashes on then off.
C. clicked Run >> clicked Start Debugging
Same result as B
Attached is a image of the Explorer window to show directory locations and break points:

There is no problem using debug for me.
You can try debugging like the picture.

After more research, I found that setting the Interpreter to Python 3.6.9-bit /usr/bin/python3 allowed VSC to enter debug mode. I was using the "Recommended" version. I don't know why VSC required the 3.6.9 version instead of the 3.7.9. I do have the 3.7.9 installed on the jetson.

Related

How to run python from different drive using Visual Studio Code

My OS is Windows 10.
I have test.py file in E drive.
I installed Python 3.7.2 and Visual Studio Code in C drive.
I set system path for Python 3.7.2 so I could run python on CMD on both of the drives.
I also set python.pythonPath in setting.json file in .vscode under my work folder.
So python.pythonPath is same as the system path.
but I can't run that test.py file on VScode.
It shows python 3.7.2 file's path and says "No such file or directory".
Is there a way to run python file on different drive using VScode?
You can use debugging to run specific files in a reusable way.
https://code.visualstudio.com/docs/python/debugging
It will create a launch.json file with contents similar to this. You can add additional
properties. VSCode is good about helping you create and edit the launch.json file.
{
// 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": "RunPython",
"type": "python",
"request": "launch",
// run a file by specifying a full path
"program": "E:/FullPathTo/FileA.py",
// OR run the file currently open in VSCode
// // "program": "${file}",
// OR run a file relative to the current folder you have open
// // "program": "${workspaceFolder}/folder/FileB.py"
"console": "integratedTerminal",
"args": ['InputArg1','InputArg2']
}
]
}
If you've already set Python as an environment variable, you can simply route to the directory where the file is present (in this case E: drive) and run the command:
python test.py
I installed Code Runner Extension and it followed my system path of python 3.7.2 automatically.
Now I can simply run my code with this short cut "Crtl+Alt+N" instead of entering "python test.py" every time on the terminal.

VScode debugger cannot import modules from site-packages

I have C:\Users\user\AppData\Roaming\Python\Python37\site-packages directory in sys.path
And while I run code via cmd it works fine.
However, when i try run it via vscode debugger, I get this:
No module named request
So, how can I fix this?
According to your description, it is recommended that you could try the following methods:
Check the python interpreter for the current VSCode.
When running python files in the cmd window, the system uses the python set by the environment variable.
About it can run in cmd, but not in VSCode.This will happen if the Python interpreter used in VSCode is different from the cmd. Reference:python-interpreter.
Try using pip to install the required module again. Use 'pip --version'(Linux and Mac:'pip -v') at the terminal to check whether the version of pip comes from the current interpreter.Use 'pip install <modules name>' to install it. Then, check whether the module is in the list by 'pip list'.
Attempt to reload VSCode.
Above, I could import the required modules from similar locations. My environment:python:3.8.3; VSCode:1.47.2; Win10.
Update:
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}

How to run python in Visual Studio Code as a main module

How to run python in Visual Studio Code as a main module?
From the command line I would use the -m switch, like
python -m program.py
I need this to make relative imports work.
Is there something I could add to the launch.json file?
If this isn't possible, I maybe need to do something with runpy see python docs, but it would be nice if vscode can do this.
Edit:
For the moment I use, as a workaround, an extra run.py file which I place outside the package I want to run. Then configure vscode to run that file:
"program": "${workspaceRoot}/../run.py"
From run.py I import the package and call its entry-point function.
The documentation for debugging a module can be found here: https://code.visualstudio.com/docs/python/debugging#_debugging-specific-app-types
All you need to do is:
Select the Python: Module debug cofiguration in VS Code
Edit the launch.json and locate the Python: Module config section and replace the value for the setting module with the module name, e.g. program
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: app",
"type": "python",
"request": "launch",
"module": "module_name.app",
"console": "integratedTerminal"
}
]
}

spawn EACCES in Visual Studio Code

I configured a new debug environment in Visual Studio Code under OS X.
{
"name": "Kivy",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "/Applications/Kivy3.app/Contents/Frameworks/python/3.5.0/bin",
"program": "${file}",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
When it runs, it said "Error: spawn EACCES"
I assume this is because my current user doesn't habe the according permission to this folder since it is under the root rather than my user folder.
I tried the 2 methods, nothing works, how to handle it?
create a soft link from that folder to my own folder, but still same error
sudo VSC, still the same
How to solve this problem?
#Albert Gao,
The path you have specified above doesn't contain the name of the python file. You need to provide the path to the file, include the file name. I believe you need to change it as follows:
"pythonPath": "/Applications/Kivy3.app/Contents/Frameworks/python/3.5.0/bin/python",
If that doesn't work, then type the following into your command (terminal) window:
- which python
- Next copy that path and paste it into settings.json
If you are getting the spawn error above while using OpenOCD for the Raspberry Pi Pico, make sure that your "cortex-debug.openocdPath" in "settings.json" is set to "<Path_to_openocd_executable>/openocd" for example:
"cortex-debug.openocdPath": "/home/vbhunt/pico/openocd/src/openocd", "cortex-debug.gdbPath": "/bin/gdb-multiarch"
This is a specific instance for the Raspberry Pi Pico of #Albert Gao's excellent answer.

VSCode doesn't show python script error output

Using Python3 with Visual Studio Code (Python extension installed) within Ubuntu 16.04.
I have some basic script written:
def mainMethod():
what()
#connectToDevice()
if __name__ == "__main__":
mainMethod()
When I debug this in Visual Studio Code by hitting F5 I can't see any output with the error in Debug Console:
Traceback (most recent call last):
File "main.py", line 9, in
mainMethod()
File "main.py", line 5, in mainMethod
what()
NameError: name 'what' is not defined
If I run python3 main.py in console the output appears.
How can I see those errors in VSCode and avoid switching back and forth between it and console?
I still can't see the output in Debug Console all the time but I can see it in the Integrated Terminal by setting this option in launch.json file of VSCode. The file looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python virtual env",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"console": "integratedTerminal",
"program": "${workspaceRoot}/main.py",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
],
"pythonPath": "${workspaceRoot}/env/bin/python3"
}
]
}
The core line is "console": "integratedTerminal". Also please note that this configuration is using the interpreter from the Virtual Environment folder and the file which starts is always main.py instead of the default option which runs the file from the active editor.
EDIT: it seems that later versions solved this problem so above workaround doesn't apply anymore
In the Feb 2018 update to VS Code this issue appears to be entirely resolved. So you don't need to make any changes to the default config, becuase the integrated debug terminal is finally printing errors when they occur.
A slightly better way, for me, is to just add "console": "externalTerminal" rather than internalTerminal. That opens a new terminal when debugging, but it's a clearer one and it closes almost automatically.
I have a similar problem in my case was the Python Linter didn't show the errors.
My mistake was, that I open the VS Code when a VirtualEnv was active. Whit the command
code .
To fixed I close VS Code, delete de .vs folder, deactivate de VirtualEnv, then open VS Code and then open VirtualEnv.
And done!, problem solve. Other solution in my case would be install the linter (if I remember right, pylint is the default linter for vscode).

Categories