Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Python module using VS Code by pressing F5

My project structure is as follows:

Project Folder
--setup.py
----Module Folder
------ __init__.py
------ __main__.py

My __main__.py file contains the entry point to my application and the setup file is configured like this:

from setuptools import setup

setup(name='my_project',
      version='0.1.0',
      packages=['my_project'],
      entry_points={
          'console_scripts': [
              'my_project= my_project.__main__:main'
          ]})

This means I can run my code without the debugger attached using:

python -m my_project

I've tried debugging using VS Code by navigating to my __main__.py file and pressing F5 to run but this doesn't work and throws an exception. How do I configure Visual Studio Code to run this module in debug mode? Also how do I ensure the program also runs the module and not the file I am looking at when I press F5?

like image 282
James Mundy Avatar asked Oct 17 '22 10:10

James Mundy


2 Answers

The accepted answer didn't work for me (VSCode 1.49.0), and I got an error message reading: Invalid message: "program", "module", and "code" are mutually exclusive.

Removing the "program": "${file}", line solved the issue. I think this makes sense, since defining both a module (with an implied entrypoint) and a file is redundant.

My launch.json looks like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Module",
            "type": "python",
            "request": "launch",
            "console": "integratedTerminal",
            "module": "my_project",
        }
    ]
}
like image 91
gris_martin Avatar answered Oct 21 '22 02:10

gris_martin


After some research I've found a solution:

  1. Navigate to the top right section in the debug menu and click the cog to create a launch.json file for this project. This will be used to configure VS Code.

Configure VS Code

  1. If there isn't one already a launch.json file be created, into it paste this:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Module",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "pythonPath": "${config:python.pythonPath}",
            "module": "my_project",
            "cwd": "${workspaceRoot}",
        }
    ]
}

I found this code here: https://github.com/DonJayamanne/pythonVSCode/issues/518#issuecomment-260838308

  1. Just using this answer didn't work for me though and I got the error: No module named my_projectbut I found this answer: https://github.com/DonJayamanne/pythonVSCode/issues/826 In it the final comment tells you add the following to the config.

    "env": {"PYTHONPATH":"${workspaceRoot}"},
    

This fixes the error and now you can press F5 and your module will be debugged.

like image 20
James Mundy Avatar answered Oct 21 '22 02:10

James Mundy