skip to Main Content

I am doing the following:

  1. mkdir folder_structure
  2. mkdir folder_structure/utils
  3. touch folder_structure/utils/tools.py
  4. touch folder_structure/main.py
  5. Write in main.py:
from folder_structure.utils.tools import dummy

if __name__ == '__main__':
    dummy()
  1. Write in tools.py:
def dummy():
    print("Dummy")
  1. touch folder_structure/__init__.py

  2. touch folder_structure/utils/__init__.py

  3. Run the vs code debugger

I’m getting:

Exception has occurred: ModuleNotFoundError
No module named 'folder_structure'
  File "/Users/davidmasip/Documents/Others/python-scripts/folder_structure/main.py", line 1, in <module>
    from folder_structure.utils.tools import dummy

I have the following in my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "env": {
                "PYTHONPATH": "${workspaceRoot}"
            }
        }
    ]
}

How can I import a local module when using the debugger?

This works for me:

python -m folder_structure.main   

2

Answers


  1. Chosen as BEST ANSWER

    Changing the launch.json to this:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: Module",
                "type": "python",
                "request": "launch",
                "module": "folder_structure.main",
                "justMyCode": true
            },
            {
                "name": "Python: Current File",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal",
                "env": {
                    "PYTHONPATH": "${workspaceRoot}"
                },
                "cwd": "${workspaceRoot}",
            }
        ]
    }
    

    and debugging the module works.


  2. You need replace

    from folder_structure.utils.tools import dummy
    

    With

    from utils.tools import dummy
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search