【发布时间】:2016-03-02 14:51:55
【问题描述】:
当我以我认为应该这样做的方式从子模块导入时,我得到一个ImportError。
我有以下包裹:
pkg/
__init__.py
cow.py
pizza.py
pkg.py
components/
components.py
otherstuff.py
__init__.py
cow.py:
print "Hello"
from components import foodle
components.py:
foodle=5
而__init__ 是空的。
我无法把东西放在正确的地方或正确地组织它们。什么时候,从 pkg 目录,我尝试
from pkg import foodle
我收到“ImportError: cannot import name foodle”
排列文件和从子模块导入的正确方法是什么?我已阅读 How to import python file from git submodule ;我曾尝试在components/__init__.py 和cow.py 中弄乱sys.path,但无济于事。
这个包是在 git 上共享的,所以它需要是可移植的。 components 实际上是一个 git 子模块。
将 from components import * 放入 components/ 中的 __init__py 似乎可行,但我认为通常该文件保持为空。
【问题讨论】:
标签: python git-submodules python-import