VSCode python debug: "No module named xx" when using module attribute

Viewed 13991

My Python project folder structure is as follow:

.python_practice
|--lib
    |--lib.py
    |--__init__.py
|--practice1
    |--my_module.py
    |--__init__.py
|--__init__.py

My launch configuration is

{
    "name": "Python: Module",
    "type": "python",
    "request": "launch",
    "module": "practice1.my_module",
    "console": "integratedTerminal"
 },

In my_module.py

from lib.lib import util_func

When I try to debug using VScode I get an error: "No module named practice1.my_module"

But when I run it with the following command it works fine

python -m practice1.my_module

How could I debug my code so that the relative import work?

Any help will be appreciated, thank you.

3 Answers

This is a known bug involving debugging sub-modules.

You can try the following:

{
    "name": "Python: A name example",
    "type": "python",
    "request": "launch",
    "program": "practice1/my_module.py",
    "env": {
        "PYTHONPATH": "${workspaceFolder}"
    },
}
  • Using program instead of module is almost the same.
  • You need to solve the imports as if you had the package installed. Therfore you need to add a PYTHONPATH.

I have ran into the same issue. You may have two Python interpreters installed and only one has the modules you need. Try selecting another Python version by running from the command pallet (Ctl+Shift+p) then python: Select Interpreter and choose where your python lives. I had:

/usr/bin/python3
/usr/local/bin/python
Related