I’m working on a Python project on Windows 11 using VSCode.
I recently reorganized my project by moving all files from a subfolder to the root folder so that the code can be executed directly from the root.
Here is an example of the structure before I moved the files :
root_folder/
sub_folder/
package1/
__init__.py # empty
script1.py
package2/
__init__.py # empty
script2.py
main.py
And here is an example of the structure after I did :
root_folder/
package1/
__init__.py # empty
script1.py
package2/
__init__.py # empty
script2.py
main.py
Before the reorganization, an import in 'script1.py' was as follows :
from sub_folder.package2.script2 import example_func
After the reorganization, I changed it to :
from root_folder.package2.script2 import example_func
My problem is that although the code runs correctly when I execute :
python -m root_folder.main
In VSCode, the import is highlighted in yellow with the warning :
Import "root_folder.package2.script2" could not be resolved
I’m new to programming. All questions I found talked about a specific case or about importing a library instead of self-made code.
The issue seems related to how VSCode/Pylance analyzes paths. So how can I make it so that it correctly recognizes imports after I moved my files to the root folder ?
__init__.pyfile into the root folder itselffrom package2.script2 import example_func