I am attempting to use the jsonschema package to validate uploaded JSON payloads from the user.
I have run pip install jsonschema inside my venv and received a message confirming it's installation.
Running pip freeze confirms this.
When I attempt to import the package VS Code underlines the package with a yellow squiggly and says "Import "jsonschema" could not be resolved from sourcePylance"
I am able to access the validate function via autocomplete menus, however when I attempt to run via the VS Code run menu it fails with the following message
"ModuleNotFoundError: No module named 'jsonschema'"
If I run it manually from my terminal it executes the code as expected.
My launch.json is provided
{
// 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": "app.py",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
I think this is caused by you choosing incorrect Python interpreter.
You can use "Ctrl+Shift+P" and type "Python: Select Interpreter" to choose the interpreter where you installed this package.
Related
I'm trying to use the debugger from vscode for a specific python project. The terminal command that I use to run this python package is as followed.
synthtiger -o ./outputs/SynthDoG_adapt -c 5 -w 4 -v template.py SynthDoG config_address.yaml
The command uses a python package that can be installed by running pip install synthtiger and uses some inputs given by the arguments after the synthtiger command. The vscode launch configuration that im currently using is as followed.
{
// 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: synthdog",
"type": "python",
"request": "launch",
"program": "synthtiger",
"python": "/home/XXX/anaconda3/envs/synthdog/bin/python",
"args": [
"-o",
"./outputs/SynthDoG_adapt",
"-c",
"5",
"-w",
"4",
"-v",
"-template",
"SynthDog",
"config_address.yaml"
],
"console": "integratedTerminal",
"justMyCode": true
}
]
}
If I then run the debugger from vscode, I get the following error.
FileNotFoundError: [Errno 2] No such file or directory: '/mnt/d/projects/data_generation/synthdog/synthtiger'
Which indiactes that the python file is not found, which is also correct since I installed the python package with pip. The python package it builds upon is listed here. The default command that one can use to test this approach is detailed here, such as synthtiger -o results -w 4 -v examples/synthtiger/template.py SynthTiger examples/synthtiger/config_horizontal.yaml and requires the github folder in the current directory.
The question I have is how do I adapt the launch.json to run the synthiger command instead of trying to search for a python file called synthtiger. I was hoping for a solution something in the range of changing the type to shell command.
Thanks for taking a look.
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}",
}
]
}
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"
}
]
}
Using gdb from the command line I'm able to break when the shared library is loaded. How can I get the same behavior in VS Code knowing that I have the source code of the shared library?
For me it works somehow.
Here's my setup:
Ubuntu 18.04, debugging a C++ shared library I load from Python3 (more specifically - via Cython, but IIRC it worked equally well when loading a .so through ctypes, also I remember it working when debugging a pure C lib in a similar setup)
in VSCode I have a project that compiles into that .so
there I put a bunch of breakpoints
I created a launch configuration (text below)
also, I've compiled the .so with debugging information
here's my launch.json (for the most part, it's boilerplate, I only filled in the "program" and "args" parts and set up PYTHONPATH environment var).
note: it seems to be important to have "stopAtEntry:false" (which it is by default), otherwise VSCode tries to locate an entry .c file or something.
{
// 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": "(gdb) Launch 1123",
"type": "cppdbg",
"request": "launch",
"program": "/home/sergey/anaconda3/bin/python",
"args": [
"/storage/projects/cython-vst-loader/cython_vst_loader/test_load_plugin.py"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "PYTHONPATH",
"value": "/storage/projects/cython-vst-loader"
}
],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
anyway, in this setup I see my VSCode showing the execution stopping on my breakpoints
Unfortunately there isn't a way to flow from Python code into C code for debugging purposes (only Visual Studio has that ability to my knowledge).
Thank you #user1312695, your method works for me!
I was able to step into pybullet.c now! So let me take this as an example.
https://github.com/bulletphysics/bullet3
I want to install the debuggable version pybullet package into a conda environment and use VScode to start my debugging, here is what I did:
(0) create a new conda environment called debug_pybullet.
(1) modify cmake/FindPythonLibs.cmake
FindPythonLibs.cmake can't recognize conda environments, so after it found the wrong _PYTHON_EXECUTABLE, I need to manually set the path at around line 143:
set(_PYTHON_EXECUTABLE /home/MyName/anaconda3/envs/debug_pybullet/bin/python)
(2) modify CMakeLists.txt
I need to manually add the definitions which are included in file build_cmake_pybullet_double.sh, except the definition of CMAKE_BUILD_TYPE=Release (I prefer to let VSCode control this definition).
also I manually set PYTHON_SITE_PACKAGES at around line 93:
set(PYTHON_SITE_PACKAGES /home/MyName/anaconda3/envs/debug_pybullet/lib/python3.6/site-packages)
(3) Create launch.json in VScode.
Here is my version of launch.json:
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/MyName/anaconda3/envs/debug_pybullet/bin/python",
"args": [
"/home/MyName/<path_to_python_file>/main.py"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
}
]
}
Since I manually set the path, I don't need the environments and setupCommands here.
(4) In VScode, build all.
(5) Install the debuggable package:
$ source activate pybullet_debug
(pybullet_debug)$ pip install -e .
(6) Set the breakpoints in pybullet.c.
(7) Press F5, Run the python, and here we are!
A screenshot attached.
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"
}
]
}