简体   繁体   中英

VSCode “unresolved import” error for subfolders

I have a project with the following folder layout:

app/ #actual python aplication code
|-- package1/
    |-- __init__.py
    |-- Class1.py
|-- package2/
    |-- __init__.py
    |-- Class2.py
|-- app.py # application entrypoint
venv/ #virtenv generated from python3
Dockerfile #release dockerfile

Everything works fine from the image generated via Dockerfile running python app.py and I can execute and debug the application from inside VSCode simply by right-clicking on app.py and choosing the appropriate command. Buuuut,VSCode is showing a warning message about unresolved import as app.py . The relevant app.py code snippet is as follows

import package1
import package2

Nothing breaks, everything runs fine. It just seems to me that VSCode doesn't understand that app.py is not at the root folder, so the python interpreter should not try to import from the root folder. Is there any configuration I am missing?

The issue is whatever tool is warning you about your imports doesn't know that app is the anchor point for your code and not ../app as you opened as your workspace. You will need to either open app as your workspace or tell the extension that app/ is where Python should start looking (typically via PYTHONPATH in a .env file).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM