在从它们导入之前,我已经将目录添加到我的 sys.path 数百次,并且知道从中可以期待什么。我承认,我没有很多包装或鸡蛋的经验。
但是,我有一个有点特殊(显然)的目录不起作用。
回溯,当我尝试导入它时,看起来像:
Traceback (most recent call last):
File "t", line 17, in <module>
from InfinityUnixHost.activities.ICWrap import ICWrap
File "/home/dstromberg/PycharmProjects/infinity_PY/src/InfinityUnixHost/activities/ICWrap.py",
line 8, in <module>
from InfinityUnixHost.IPGlobal import DB_POOL
File "/home/dstromberg/PycharmProjects/infinity_PY/src/InfinityUnixHost/IPGlobal.py",
line 12, in <module>
INTERNAL_VERSION = pkg_resources.require("InfinityUnixHost")[0].version
File "/home/dstromberg/virtualenvs/dev/local/lib/python2.7/site-packages/pkg_resources/__init__.py",
line 941, in require
needed = self.resolve(parse_requirements(requirements))
File "/home/dstromberg/virtualenvs/dev/local/lib/python2.7/site-packages/pkg_resources/__init__.py",
line 828, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'InfinityUnixHost'
distribution was not found and is required by the application
一个产生这个错误的小测试程序是:
#!/usr/bin/env python
import sys
import os.path
sys.path.insert(0, os.path.abspath('src'))
sys.path.insert(0, os.path.abspath('eggs/InfinityCore-6-py2.7.egg'))
#for directory in sys.path:
# # print(directory)
# if \
# os.path.exists(os.path.join(directory,
'InfinityUnixHost')) and \
# os.path.exists(os.path.join(directory,
'InfinityUnixHost/activities')) and \
# os.path.exists(os.path.join(directory,
'InfinityUnixHost/activities/ICWrap.py')):
# print('found in %s' % directory)
# from InfinityUnixHost.activities.ICWrap import ICWrap
from InfinityUnixHost.activities.ICWrap import ICWrap
dummy = ICWrap
顺便说一句,注释掉的 for 循环可以看到我尝试导入的文件(未注释时),但是导入机制仍然没有加载模块。
遗憾的是,我无法分享有关 InfinityUnixHost 内部结构的太多细节,但我们可以根据需要进行讨论。
有谁知道为什么导入以这种奇怪的方式失败?它甚至不是 ImportError!
顺便说一句,我一直运行测试代码直到 strace,我可以看到它打开了 ICWrap.py,但它似乎仍然无法将 ICWrap.py 加载到解释器中。
谢谢!