甚至在导入时都会创建.pyc文件

时间:2016-09-19 06:33:44

标签: python python-2.7 numpy

我有两个名为numbers.pynumpyBasicOps.py的Python文件。 numbers.py是一个简单的Python文件,不导入任何模块。 numpyBasicOps.py导入numpy库。

每当我运行numpyBasicOps.py时,首先显示numbers.py的输出,然后显示与numpy模块相关的一些错误:

Traceback (most recent call last):
  File "./numpyBasicOps.py", line 3, in <module>
    import numpy as np
  File "/Library/Python/2.7/site-packages/numpy-1.11.2rc1-py2.7-macosx-10.11-intel.egg/numpy/__init__.py", line 142, in <module>
    from . import add_newdocs
  File "/Library/Python/2.7/site-packages/numpy-1.11.2rc1-py2.7-macosx-10.11-intel.egg/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/Library/Python/2.7/site-packages/numpy-1.11.2rc1-py2.7-macosx-10.11-intel.egg/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/Library/Python/2.7/site-packages/numpy-1.11.2rc1-py2.7-macosx-10.11-intel.egg/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/Library/Python/2.7/site-packages/numpy-1.11.2rc1-py2.7-macosx-10.11-intel.egg/numpy/core/__init__.py", line 22, in <module>
    from . import _internal  # for freeze programs
  File "/Library/Python/2.7/site-packages/numpy-1.11.2rc1-py2.7-macosx-10.11-intel.egg/numpy/core/_internal.py", line 15, in <module>
    from .numerictypes import object_
  File "/Library/Python/2.7/site-packages/numpy-1.11.2rc1-py2.7-macosx-10.11-intel.egg/numpy/core/numerictypes.py", line 962, in <module>
    _register_types()
  File "/Library/Python/2.7/site-packages/numpy-1.11.2rc1-py2.7-macosx-10.11-intel.egg/numpy/core/numerictypes.py", line 958, in _register_types
    numbers.Integral.register(integer)
AttributeError: 'module' object has no attribute 'Integral'

另外,我看到生成.pyc的{​​{1}}文件。

numbers.py文件如果未在numbers.pyc中导入,以及为什么显示numpyBasicOps.py的输出,会如何生成?

1 个答案:

答案 0 :(得分:2)

numpy将自己的类似整数的对象注册为实现抽象基类numbers.Integral。为此,必须使用import numbers来访问该对象。

或至少,它试图失败;当您将模块numbers 命名为时,它也被导入了。换句话说,您的numbers.py模块屏蔽了内置standard library module numbers

将您的模块重命名为其他内容,并确保删除已创建的numbers.pyc文件。