python - .pyc 文件在甚至没有导入时就被创建

标签 python python-2.7 numpy

我有 2 个 Python 文件,名为 numbers.pynumpyBasicOps.pynumbers.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'

此外,我还看到正在生成 numbers.py.pyc 文件。

即使没有在 numpyBasicOps.py 中导入 numbers.pyc 文件,它也是如何生成的以及为什么会输出 numbers.py > 显示?

最佳答案

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

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

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

关于python - .pyc 文件在甚至没有导入时就被创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39566724/

相关文章:

python - 溢出错误 : cannot convert float infinity to integer

python-2.7 - "pip install json"在 Ubuntu 上失败

arrays - 添加维度numpy数组

python - Pandas 数据框到字典的字典

python - 预期关键数据类型不匹配 : S actual: L Dynamodb insert error with boto3

python - BeautifulSoup 网络抓取 find_all() : custom function not working

python - Django - ValidationError 不显示

python - 反转数据框中的行值

c++ - 在 c++ boost python 列表中附加整数时出错

python - 3D 中的 Numpy 网格