Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a Python module in Visual Studio Code's launch.json

My question may seem simple but, I have a module that I launch in a terminal like this:

python -m my_module.my_file 

How do I debug this in Visual Studio Code?

I have this in my launch.json (documentation)

"type": "python", "request": "launch", "pythonPath": "D:\\ProgramData\\Anaconda3\\envs\\simulec\\python.exe", "args": [    "-m",    "my_module.my_file", ] 

If I don't set the program option or if I set it to "" I get "File does not exist" Error.

How can I fix this?

like image 228
dzada Avatar asked Sep 07 '17 17:09

dzada


People also ask

How do I debug Python code in Visual Studio?

Python in Visual Studio supports debugging without a project. With a stand-alone Python file open, right-click in the editor, select Start with Debugging, and Visual Studio launches the script with the global default environment (see Python environments) and no arguments.

How do I debug a json file?

Try putting your JSON file's content into a validator, like http://jsonlint.com/ or http://jsonformatter.curiousconcept.com/ and see what it says. Show activity on this post. I suggest entering your JSON into a linter such as http://jsonlint.com/.


2 Answers

Actually, there is a very simple option to do this I found by accident while trying to edit the launch.json file.

"type": "python", "request": "launch", "pythonPath": "D:\\ProgramData\\Anaconda3\\envs\\simulec\\python.exe", "module": "my_module.my_file", 

Simply specify the module in the module key "module": "my_module.my_file"

The -m is not useful any more.

like image 58
dzada Avatar answered Sep 24 '22 09:09

dzada


In a terminal like this: python -m xyz abc.z3

(Please make sure you are opening "the root folder of your project").

{     // 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": "module",             "type": "python",             "request": "launch",             "module": "xyz",             "args": [                 "abc.z3"             ]         }     ], } 
like image 37
madeinQuant Avatar answered Sep 22 '22 09:09

madeinQuant