Reputation: 727
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)
What's the problem?
Upvotes: 1
Views: 789
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