Search code examples
pythonpytestpython-importpython-module

Why can't I name a module 'array'?


Given the following directory structure:

app/
└── array/
    ├── test/
    │   ├── __init__.py
    │   └── test_array.py
    ├── __init__.py
    └── functions.py

When I run pytest from the app directory, I get the following error:

ERROR collecting array/test/test_array.py _______________________________________
ImportError while importing test module '/path/to/app/array/test/test_array.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/[email protected]/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
E   ModuleNotFoundError: No module named 'array.test'; 'array' is not a package

Simply renaming the module to arrays works, but I don't understand what's wrong with array.


Solution

  • Because it collides with the stdlib module of the same name.