AnujaP
AnujaP

Reputation: 127

.pyc file gets created when it is not even imported

I have 2 Python files called numbers.py and numpyBasicOps.py. numbers.py is a simple Python file, not importing any module. numpyBasicOps.py imports the numpy library.

Whenever I run numpyBasicOps.py, numbers.py's output is displayed first followed by some error relating to numpy module:

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'

Also, I see a .pyc file for numbers.py being generated.

How does the numbers.pyc file get generated even when it is not imported in numpyBasicOps.py and why is output for numbers.py displayed?

Upvotes: 2

Views: 187

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122392

numpy registers their own integer-like objects as implementing the Abstract Base Class numbers.Integral. To do this, it must use import numbers to get access to that object.

Or at least, it tried to and failed; as you named your module numbers as well it was imported instead. In other words, your numbers.py module masked the built-in standard library module numbers.

Rename your module to something else, and make sure you delete the numbers.pyc file that was created.

Upvotes: 2

Related Questions