import pandas
发出类似以下消息时会造成混乱:
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/raf/anaconda3/lib/python3.7/site-packages/pandas/__init__.py", line 19, in <module>
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['pytz']
真正的错误实际上不在pytz
中,而是在pytz
所要求的缺少的库中:
>>> import pytz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/__init__.py", line 20, in <module>
from pytz.tzinfo import unpickler, BaseTzInfo
File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/tzinfo.py", line 4, in <module>
from bisect import bisect_right
ImportError: cannot import name 'bisect_right' from 'bisect' (/home/raf/tmp/bisect.py)
如您所见,我恰好在试图插入bisect.py
的同一文件夹中有一个名为import pandas
的文件,但是pandas
引发的错误消息却没有反映这一点。毕竟这是一个愚蠢的错误,但是让我头疼的要解决这个问题。
在pandas/__init__.py
中追溯其来源,请注意在e
之后没有使用原始错误except ImportError as e
。我认为应该。
for dependency in hard_dependencies:
try:
__import__(dependency)
except ImportError as e:
missing_dependencies.append(dependency)
if missing_dependencies:
raise ImportError(
"Missing required dependencies {0}".format(missing_dependencies))