Debugging Python Module from within Github Repo in VSCode - python

I have a python library in github I want to debug. The library has setup.py as well as a pip released package. So I can install this module using either pip install git-sim or python3 setup.py install.
This module adds a command to the path and I can execute in terminal with git-sim .
Main Problem: I want to clone the github repo and open vscode inside the cloned repo and debug the code getting executed while I am running the command from the terminal. The breakpoints should hit the file inside the repo.
Link to the github Repo: https://github.com/initialcommit-com/git-sim
I went searching with these requirements. Best I could come up so far is
An edit mode in setup.py which can be started by pip install -e . . While I ran this command and was successful I did not see a way to debug it.
How do we debug a module in Vscode? Give this code in launch.json
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "git-sim",
"justMyCode": false,
}
But while I am running this I am getting an error, no module named git-sim even though the same command works outside the debugger in the terminal. i.e both git-sim and python3 -m git_sim
Any suggestion of where I maybe going wrong? Or if there is some alternative option I have not considered

Found the issue. I made a mistake in assuming the terminal command git-sim and the actual python -m git_sim were same.
Module Name and Terminal commands are different. So if we modify launch config from "module":"git-sim" to "module":"git_sim" it works. Modified config given below:
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "git_sim",
"justMyCode": false,
}

Related

Debugging python code that uses python package in vscode with launch.json

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.

Import "jsonschema" could not be resolved from sourcePylance

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.

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"
}
]
}

vscode do not find my custom python package

I'm new to VS Code for python development on Windows and my pylint cannot find a package.
This is my project directory structure.
workspace/ <- This is VS Code workspace (E:\workspace)
.vscode/
launch.json
settings.json
project1/
mypackge/
__init__.py <- In here, I wrote: `import mypackage.first_sub_pkg`
first_sub_pkg/
__init__.py <- In here, I wrote: `from .second_sub_pkg.mymodule import MyClass`
second_sub_pkg/
__init__.py <- In here, I wrote: `from .mymodule import MyClass`
mymodule.py <- This module has class: `MyClass`
test_script/
mytest.py
project2/
etc.../
And I wrote the mytest.py script code like:
from mypackge.first_sub_package import MyClass
I'm using C:/Anaconda3/python.exe for python interpreter
When I click the button on the upper side ▷ (Run Python File in Terminal) on the upper right side of VS Code, I get this error message
PS E:\workspace> & c:/Anaconda3/python.exe e:/workspace/project1/test_script/mytest.py
Traceback (most recent call last):
File "e:/workspace/project1/test_script/mytest.py", line 1, in <module>
from first_sub_pkg.second_sub_pkg import MyClass
ModuleNotFoundError: No module named 'first_sub_pkg'
Also, I added workspace/.vscode/launch.json like:
{
// 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",
"pythonPath": "${command:python.interpreterPath}",
"env": {
"PYTHONPATH": "${workspaceFolder};E:/workspace/project1"
}
}
]
}
And workspace/.vscode/settings.json like:
{
"python.autoComplete.extraPaths": [
"E:/workspace",
"E:/workspace/project1",
"E:/workspace/project1/first_sub_pkg",
],
"python.pythonPath": "c:/Anaconda3/python.exe",
"terminal.integrated.shell.windows": "C:/windows/System32/WindowsPowerShell/v1.0/powershell.exe",
"python.linter": "pyLint",
"python.linting.pylintPath": "pylint"
}
And my user settings.json file is like:
{
"python.autoComplete.extraPaths": [
"E:/workspace",
"E:/workspace/project1",
"E:/workspace/project1/first_sub_pkg",
]
}
I already ran this test script in Eclipse + pydev environment, and there was no problem running it.
But somehow VSC cannot import my modules.
I seems like system path problem since it works well when I run python and append 'E:/workspace/project1' to system path (import sys; sys.path.append('E:/workspace/project1');), but I cannot find out how to solve the problem. (Adding system variables in Windows settings did not worked neither).
What did I miss? Somebody please help me. I searched for 2 days but got nowhere.
first_sub_pkg is not in the same directory as the mytest.py file. You first have to move up one level to project1/ then into mypackage/ then continue with the rest of imports. So the imports you do in mytest.py should be like so:
from ..mypakage.first_sub_pkg.second_sub_pkg.third_sub_pkg.mymodule import MyClass
Why you have so many sub directories I don't know, but your directory structure will get really confusing really fast.
Keep the zen of python in mind when coding.
Solutions:
One:
change this statement :"from first_sub_pkg.second_sub_pkg import MyClass" in mytest.py
to "from mypackage.first_sub_pkg.second_sub_pkg.third_sub_pkg.mymodule import MyClass".
Two:
change 'env' in lanuch.json from "PYTHONPATH": "${workspaceFolder};E:/workspace/project1"
to "PYTHONPATH": "${workspaceFolder};${workspaceFolder}/project1/mypackge".
Explain:
The Python only can search the paths in PYTHONPATH. If the module nested in the paths you need to use '.' to connect the folder until points to the module file.
This sulotion is not for mac.
Open vs code press ctrl+shift+p
Enter: Python:Select Interpreter
Choose your environment and run the code again. (rjz is my environment name)
If this doesn't fix, you need to use CMD for install packages with conda or pip. In my case install packages with VS code terminal doesn't fix the problem.
For some packages, you need to install with vs code terminal expect CMD.

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"
}
]
}

Categories