2

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 ?

New contributor
user31885331 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
6
  • 1
    try adding an __init__.py file into the root folder itself Commented Nov 15 at 21:40
  • Thank you for your answer. I forgot to mention but I did try that. It unfortunately didn't work. Commented Nov 15 at 21:51
  • what dir are you running the app from? Commented Nov 15 at 21:56
  • I am running the app from the directory that contains the root_folder as well as other projects. When I execute it, it looks as follows : C:\Users\name\Desktop\projects>python -m root_folder.main Commented Nov 15 at 22:00
  • 1
    try changing the import to from package2.script2 import example_func Commented Nov 15 at 22:07

2 Answers 2

1

Remove the root_folder from all of the imports. Example:

from root_folder.package2.script2 import example_func

Becomes:

from package2.script2 import example_func

Also, run your app with

python main.py

instead of

python -m root_folder.main

This should solve your issue.

Sign up to request clarification or add additional context in comments.

Comments

0

All you need to do is to make your root_folder Python package too like your package1/ and package2/ which means you need to have an _init_.py in the root_folder too

So your structure look this below:

root_folder/
    package1/
        __init__.py
        script1.py
    package2/
        __init__.py
        script2.py
    __init__.py
    main.py
New contributor
Bashir Faruq is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.