Home  >  Q&A  >  body text

Pythonmo模块导入问题

有一个包 pack,里面有__init__.py ,test.py
若我在__init__.py中导入 test.py 采用

import test

运行__init__.py 没问题,但在包外 导入pack 出现问题,找不到test
若采用

from . import test or from pack import test

__init__.py就找不到test,包外导入pack,就没问题,这是为什么?

PHP中文网PHP中文网2741 days ago371

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:32:46

    Pay attention to python’s module search process: https://docs.python.org/2/tut...

    Run __init__.py to import test because the program runs in the current directory, that is, the pack directory is automatically added to the module search path.

    reply
    0
  • Cancelreply