简体   繁体   English

子文件夹的VSCode“未解决的导入”错误

[英]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.通过运行Dockerfile python app.py的 Dockerfile 生成的图像一切正常,我只需右键单击app.py并选择适当的命令即可从 VSCode 内部执行和调试应用程序。 Buuuut,VSCode is showing a warning message about unresolved import as app.py . Buuuut,VSCode 显示有关unresolved importapp.py的警告消息。 The relevant app.py code snippet is as follows相关的app.py代码片段如下

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.在我看来,VSCode 不理解app.py不在根文件夹中,因此 python 解释器不应尝试从根文件夹导入。 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.问题是任何警告您导入的工具都不知道app是您的代码的锚点,而不是../app作为您的工作区打开时的锚点。 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).您将需要打开app作为您的工作区或告诉扩展app/是 Python 应该开始查找的位置(通常通过.env文件中的PYTHONPATH )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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