Why does the file name struct.py give errors?

Viewed 58

I'm experimenting with image processing in python and for two days i was stuck with a problem.

I realized that naming a file to struct.py gave the error "numpy.core.multiarray failed to import" when trying the simple script below:

import numpy as np

k = np.ones((9,9))

print(k)

the same code worked when i created a file with another name.

Now I'm looking for an answer as to why that happened.

I created a test file "struct.py" to try different structuring elements for morphological operations. In all my other test files numpy worked correctly, but for some reason, in that "struct.py" script I got an error saying that "numpy.core.multiarray failed to import". I was so invested into solving that problem that during the solving process i didn't think to test if numpy worked in my other scripts (where they had worked before)

I reinstalled python several times, switched IDE, tried to code locally instead of using a version controller but all of these things seemed pretty unnecessary for me to do because i didn't see how that could be the reason for numpy not to work.

Today i saw that there is a python module called struct, and that me using that name for my script must be the cause of the problem, but I still don't understand why numpy stopped working because of that. Is numpy dependent on the struct module? How could this happen?

Also this is my first time actually posting an issue on stackoverflow, please let me know if i should change the contents of my post or delete unnecessary information or add more.

2 Answers

You must not name your module struct.py because struct module exists as built-in (this is used to serialize/deserialize data to/from memory/disk, with endianness & size management for numeric types)

>>> import struct
>>> struct.__file__
'C:\\Users\\xxxx\\AppData\\Local\\Programs\\Python\\Python37\\lib\\struct.py'

If you create a file named the same way, if another module tries to import the builtin struct module, it could import yours instead, and hard-to-understand errors may appear.

If I create a struct.py file in my current directory containing just:

print("hellooooo bug")

here what happens when I import numpy:

>>> import numpy
hellooooo bug
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\jotd6\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\__init__.py", line 140, in <module>
    from . import _distributor_init
  File "C:\Users\jotd6\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\_distributor_init.py", line 9, in <module>
    from ctypes import WinDLL
  File "C:\Users\jotd6\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 14, in <module>
    from struct import calcsize as _calcsize

as you see numpy needs struct. Just don't use that name (or any generic name, without checking if it doesn't exist as built-in first). An indirect variant of Python csv import fails

I pretty sure it is because in module numpy there is a file called pickle.py which contains a line called from struct import pack, unpack since your document is called struct but does not contain pack or unpack it gives an error due to a circular import.

Related