模块在运行文件时专门导入错误

时间:2016-08-01 04:23:02

标签: python module

我尝试在脚本中导入并使用matplotlib.pyplot,但我收到以下错误:

    Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    from matplotlib import pyplot as plt
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/pyplot.py", line 36, in <module>
    from matplotlib.figure import Figure, figaspect
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/figure.py", line 40, in <module>
    from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/axes/__init__.py", line 4, in <module>
    from ._subplots import *
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/axes/_subplots.py", line 10, in <module>
    from matplotlib.axes._axes import Axes
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/axes/_axes.py", line 22, in <module>
    import matplotlib.dates as _  # <-registers a date unit converter
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/dates.py", line 126, in <module>
    from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/dateutil/rrule.py", line 13, in <module>
    from fractions import gcd
ImportError: cannot import name 'gcd'

奇怪的是,如果我重新启动Python shell并直接导入它,我可以很好地导入它,但是一旦我尝试运行我的脚本,即使我运行脚本然后在运行脚本后导入它为了生成数据,我得到了错误。如果重要的话,进口线总是一样的:

import matplotlib
from matplotlib import pyplot

我的脚本是从桌面上的文件夹运行的,我在pip3的终端上安装了matplotlib。

1 个答案:

答案 0 :(得分:2)

名为fractions.py的文件正在影响同名的内置模块,导致其他库尝试使用该库时出现问题。将您的文件命名为其他内容。