【问题标题】:.pyc file gets created when it is not even imported.pyc 文件在未导入时被创建
【发布时间】:2016-09-19 06:33:44
【问题描述】:

我有 2 个名为 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'

另外,我看到正在为numbers.py 生成一个.pyc 文件。

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

【问题讨论】:

    标签: python python-2.7 numpy


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢@Martijn Pieters。它现在对我有用。但我有一个疑问:我目前拥有的 numpy 是新安装的。早些时候,numpy 已经存在于 Python 中。我不必单独安装它。但是为了安装 Tensorflow,这个 numpy 版本出现了问题,所以我不得不删除它。我安装了另一个与 tensorflow 兼容的 numpy 版本。所以,我的疑问是 - 对于早期版本的 numpy,文件“numpyBasicOps.py”过去可以正常工作,没有给出任何 numpy 错误。那么为什么它更早工作呢?
    • @AnujaP:这并没有太多的信息可以继续,但猜测听起来像旧版本的numpy 没有导入numbers
    猜你喜欢
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多