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.
Related
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,
}
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.
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.
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?
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"
}
]
}