VS Code奇怪的错误

时间:2018-04-15 14:38:09

标签: python visual-studio-code

我有这样的包层次结构:

dir/
    subdir1/
        __init__.py
        module3.py
        module4.py
    __init__.py
    module1.py
    module2.py

分别在module2和module4中有一个msg变量。

我在module1中导入module2,它可以工作:

import module2
print(module2.msg)

但是当我在module3中导入module4时,vscode给了我错误:[pylint] E0401:Unable to import 'module4'。但是,当我通过python .\subdir1\module3.py运行它时,python解释器并没有抱怨并顺利运行:

import module4
print(module4.msg)

enter image description here

问题是什么?

编辑:enter image description here

1 个答案:

答案 0 :(得分:1)

问题是您是否直接将模块作为文件路径执行。 Python并不知道module3包含在subdir1包中,因此无法解析导入问题。如果你python -m subdir1.module3它会工作。