简体   繁体   中英

Python can't import from the same directory?

I have the following directory structure (I didn't write this so I'm assuming it has to work somehow?):

tool.py
core/
  __init__.py
  config.py
  common.py

tool.py indirectly imports config.py , and config.py has a line from common import foo , which displays the following error:

...
  File "...\core\config.py", line 5, in <module>
    from common import foo
ImportError: No module named 'common'

It probably isn't relevant, but I'm using Python 3.4 on Windows, and the tool.py directory is in the system path (I'm simply running it as tool ).

使用相对导入:

from .common import foo

You have to use relative imports

from .common import foo

Python 3 makes a distinction between absolute and relative imports and doesn't support implicit relative imports which you could use in Python 2.x.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM