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.
Related
print line is behine the first breakpoint, but the result is already in terminal
I tryed to look https://code.visualstudio.com/docs/python/debugging but not find any similar args.
edit:
I realize that the problem will only occur when I name the file django.py.
When I rename the file to other name like website.py, it won't occur.
I wander if there are other names that will trigger this bug.
below are my launch.json
{
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
}
]
}
This is because django.py conflicts with other files.
An extract from the documentation
You’ll need to avoid naming projects after built-in Python or Django components. In particular, this means you should avoid using names like django (which will conflict with Django itself) or test (which conflicts with a built-in Python package).
I.e. some other process(es) is looking for, and running django.py. And your file is over-writing the other file.
To fix your issue, just avoid naming the file django.py or test.py
I want to set 2 interpreter Paths in VScode(Windows) for the Python extension.
I don't know how and I don't want to use .env files. I want to add Python 2.7 and 3.8
VScode settings.json:
...
"python.languageServer": "Pylance",
"python.defaultInterpreterPath": "C:\\Users\\{myuser}\\AppData\\Local\\Programs\\Python\\Python38-32\\python.exe",
"python.showStartPage": false,
...
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Aktuelle Datei",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
}
]
}
I know that there is "python.autoComplete.extraPaths" but it didn't work.
The python extension can detect the python interpreter automatically. It will automatically look for interpreters in the following locations:
1. Standard install paths such as /usr/local/bin, /usr/sbin, /sbin, c:\\python27, c:\\python36, etc.
2. Virtual environments located directly under the workspace (project) folder.
3. Virtual environments located in the folder identified by the python.venvPath setting (see General settings), which can contain multiple virtual environments. The extension looks for virtual
and so on... You can refer to the official docs for more information.
You can also manually specify an interpreter if Visual Studio Code does not locate it automatically. like this:
I found this answer very useful to understand relative imports in Python. However I have found difficulty in getting VS Code's "Current File" to play nicely with this, since it launches the file directly using ${file}.
To summarise said answer, suppose we have a structure like this:
mymod/
__init__.py
apple.py
orange.py
If orange.py imports something from apple.py:
from .apple import apple_function
then the correct way to run orange.py as a script without an ImportError is with the following command from the directory containing mymod:
python -m mymod.orange
Currently, I manually enter a command similar to the above into the terminal, which works, but I'd prefer to use a keyboard shortcut that works regardless of filename and saves me some typing.
Is there anything I can add to the launch.json to have a configuration that launches python for each particular file as above automatically?
Use the command extension.commandvariable.file.relativeDirDots from the extension Command Variable
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"console": "integratedTerminal",
"module": "${command:extension.commandvariable.file.relativeDirDots}.${fileBasenameNoExtension}",
}
]
}
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"
}
]
}
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).