Bicheng
Bicheng

Reputation: 727

VS Code weird error

I have a hierarchy of packages like this:

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

There is a msg variable in module2 and module4 respectively.

I import module2 in module1, and it works:

import module2
print(module2.msg)

But when I import module4 in module3, vscode gives me the error: [pylint] E0401:Unable to import 'module4'. However, when I run it by python .\subdir1\module3.py, python interpreter doesn't complain this and run smoothly:

import module4
print(module4.msg)

enter image description here

What's the problem?

EDIT: enter image description here

Upvotes: 1

Views: 789

Answers (1)

Brett Cannon
Brett Cannon

Reputation: 15980

The problem is you're executing the module directly as a file path. Python doesn't know module3 is in the subdir1 package, so it can't resolve the import. If you did python -m subdir1.module3 it will work.

Upvotes: 1

Related Questions