You can replace your main.py code with this code:
import sys
from pprint import pprint
pprint(sys.path)
When you click the Run Button, In fact, you are executing this command:
python debug_example\main.py
You can get it from the terminal outputs.
And through this, the parent folder path of main.py -> xxx\debug_example will be added to the PYTHONPATH.
When you execute python -m debug_example.main command, the parent folder path of debug_example will be added to the PYTHONPATH.
As you import through this:
from debug_example.src.util import my_util
The parent folder of debug_example needed to be in the PYTHONPATH. This is the reason why clicking Run Button does not work while executing the command in the terminal works, but if you execute the command python debug_example\main.py will not work too.
Solution:
The PYTHONPATH environment variable specifies additional locations
where the Python interpreter should look for modules. In VS Code,
PYTHONPATH can be set through the terminal settings
(terminal.integrated.env.*) and/or within an .env file.
When the terminal settings are used, PYTHONPATH affects any tools that
are run within the terminal by a user, as well as any action the
extension performs for a user that is routed through the terminal such
as debugging. However, in this case when the extension is performing
an action that isn't routed through the terminal, such as the use of a
linter or formatter, then this setting will not have an effect on
module look-up.
When PYTHONPATH is set using an .env file, it will affect anything the
extension does on your behalf and actions performed by the debugger,
but it will not affect tools run in the terminal.
If needed, you can set PYTHONPATH using both methods.
official docs.
Add this in the settings.json file:
"terminal.integrated.env.windows": {
"PYTHONPATH": "${workspaceFolder};"
},
Through this, both click Run Button and debugging will work.
And add this in the launch.json:
"env": {
"PYTHONPATH": "${workspaceFolder};"
},
Or
create a .env file under the workspace folder then add this in it:
PYTHONPATH= { the path of workspace folder}
Both only work for debugging.