Search code examples
pythonvisual-studio-codepackagevscode-debugger

VSCode Python - Debug package


I have had an issue I couldn't resolve now for a while, and I feel like it's a trivial issue when it comes to Debugging packages using VScode. When I have a complex project I set up a setup.py file and create a package, I use this package inside of my package to reference to different modules. For example:

.
└── project_root
    ├── module1
    │   ├── f1.py
    │   ├── f2.py
    │   └── __init__.py
    ├── module2
    │   ├── f1.py
    │   ├── f2.py
    │   └── __init__.py
    └── module3
        ├── f1.py
        ├── f2.py
        └── __init__.py

############# IMPORTING CLASSES/METHODS ##################
### for example inside modules3.f1:
from project_root.module1.f2 import some_class

I always use a virtual environment. If I install my setup file with pip install . the importing of the file works as expected. Unfortunately, when it comes down to debugging, it will only respect the breakpoints of the files inside my virtual environment <virtual env name>/lib64/python3.8/site-packages/<package_name> and not the ones I'm working on in the project_root.

So I have basically 2 questions:

a) Is there a way to tell VSCode to use my <project_root> files instead of the virtual env package for my package?

b) I'm creating a package because otherwise, I have problems referencing files in parent directories. Is there a better approach to creating complicated code structures than immediately setting up a setup.py and ultimately resolving my debugging issue?

My VScode launch.json is setup like this:

{
    // 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",
            "cwd": "${fileDirname}",
            "args": ["-d"],
            "justMyCode":false  
            // add env
            "env": {
                "MODE": "True"
            }
        }
    ]
}

############ UPDATE ############################## I hope this image describes my problem better. I setup virtual environment, set breakpoints in the Identical files, in my "working directory" and inside the virtual env (v/lib/python3.8/site-packages/Packagename/module1/f1.py). Then I execute my script in the debugger and the breakpoint in the V env will activate and not the one in the working dir. enter image description here


Solution

  • Thank you @JialeDu for pointing me in the right direction.

    The solution i was looking for can be found in this post: Visual Studio Code - How to add multiple paths to python path?

    Solution

    Answer to this problem is to remove the package from the Libary like stated below by @JialeDu and update the launch.json file. Update the launch.json file to:

    {
        // 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",
                "cwd": "${workspaceFolder}",    //  <- THIS NEEDS TO BE 'workspaceFolder'
                "justMyCode":false,
                // add env
                "env": {
                    "PYTHONPATH": "./"  // <- this is Equivalent to 'import sys; sys.path.append("./")'
                }
            }
        ]
    }
    

    Setting up launch.json this way gives you access to all the modules in your Workspace directory.