When you have module structure as follows in your project
- util
- log.py
- desktop
- mainwindow.py
and you want mainwindow.py to be able to access log.py using from util import log then you need to correctly set PYTHONPATH in VSC because that’s not automatically handles like in PyCharm where you have the Set Sources Root menu item. You can do that using the following steps:
(1) Create file .env in your root project folder with following contents:
PYTHONPATH=
(1) Create file (if not already there) settings.json in .vscode folder of your root project
(2) Add the following contents to settings.json:
{
"terminal.integrated.env.osx": {
"PYTHONPATH": "${workspaceFolder}/src"
},
"python.envFile": "${workspaceFolder}/.env"
}
(3) The terminal.integrated.env.osx sets the correct PYTHONPATH when you run your Python file in the terminal (using the play button in the top right). The python.envFile setting points to a .env file in your project’s root folder where you set the PYTHONPATH again so that VSC correctly finds all your modules while programming.
References
https://stackoverflow.com/questions/53653083/how-to-correctly-set-pythonpath-for-visual-studio-code