不应该尝试:__init__中的__import__重新引发原始异常吗?

时间:2018-11-23 11:07:46

标签: pandas

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))

问题也在这里提出: https://github.com/pandas-dev/pandas/issues/23868

0 个答案:

没有答案