Subpackages and relative imports in PyCharm

。_饼干妹妹 提交于 2019-12-01 22:19:43

foo is the directory which contains everything, including start.py

So when from start.py you do this

from foo.bar2.mod2 import mod2_f

python looks for a foo module (foo is a module because it contains __init__.py), which too high in your directory structure. I suppose it works from the IDE because IDE adds every module directory to pythonpath. But not from command line it doesn't.

simple fix since bar2 is a directory at the same level as start.py:

from bar2.mod2 import mod2_f

note that from works differently in python 3. See ImportError on python 3, worked fine on python 2.7, that's probably why PyCharm complains when fixing the import line. You should configure PyCharm so it uses Python 2 and not Python 3 for it to work, or just drop the from syntax altogether and do:

import bar2.mod2.mod2_f
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!