Subpackages and relative imports in PyCharm

前端 未结 1 382
暖寄归人
暖寄归人 2021-01-20 21:25

I am using python 2:

python --version
Python 2.7.13 :: Continuum Analytics, Inc.

I have the following project structure:

.
         


        
相关标签:
1条回答
  • 2021-01-20 22:03

    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
    
    0 讨论(0)
提交回复
热议问题