之前我已经将目录添加到我的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加载到解释器中。谢谢!
答案 0 :(得分:0)
事实证明这是因为某些模块级代码正在做一些追溯的事情,所以看起来导入机器似乎遇到了麻烦。