This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: importlib.utils.find_spec() has issues with "tests" folder
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, danielnoord
Priority: normal Keywords:

Created on 2022-01-03 12:21 by danielnoord, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
module_loader.zip danielnoord, 2022-01-03 12:21 Zip archive with the folder structure as discussed in the post.
Messages (3)
msg409579 - (view) Author: Daniël van Noord (danielnoord) * Date: 2022-01-03 12:21
Affected folder structure:
── module_loader
│   ├── __init__.py
├── pyproject.toml
├── setup.cfg
└── tests
    └── test_module_loader.py

```console
❯ cat '/Users/daniel/DocumentenLaptop/Programming/Test/module_loader/module_loader/__init__.py'
from importlib import util


def load_a_spec(modname):
    return util.find_spec(modname)
```

```console
❯ cat '/Users/daniel/DocumentenLaptop/Programming/Test/module_loader/tests/test_module_loader.py'
from module_loader import load_a_spec


def test_me():
    load_a_spec("tests")
```

When I run `pytest` from within the `module_loader` directory and inspect what `load_a_spec` returns I find that it returns:

ModuleSpec(name='tests', loader=<_frozen_importlib_external.SourceFileLoader object at 0x10a4b4820>, origin='/Users/daniel/DocumentenLaptop/Programming/Github/astroid/tests/__init__.py', submodule_search_locations=['/Users/daniel/DocumentenLaptop/Programming/Github/astroid/tests'])

It seems to take the `tests` folder from another project, in this case astroid, and returns its spec. 
This is my `sys.path`, which might be relevant:

```console
for i in sys.path:print(i)
/Users/daniel/DocumentenLaptop/Programming/Test/module_loader/tests
/usr/local/bin
/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python39.zip
/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9
/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload
/Users/daniel/Library/Python/3.9/lib/python/site-packages
/usr/local/lib/python3.9/site-packages
/Users/daniel/DocumentenLaptop/Programming/Github/astroid
/Users/daniel/DocumentenLaptop/Programming/Github/DanielNoord/docstringformatter
/Users/daniel/DocumentenLaptop/Programming/Test/module_loader
/usr/local/Cellar/pybind11/2.8.1/libexec/lib/python3.9/site-packages
```

Let me know if I'm doing anything wrong, but I would expect the returned spec to be from `module_loader` instead of another project.
msg409809 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2022-01-05 23:10
This is working as intended. Because you don't have a `__init__.py` file in your `tests` directory, Python considers it a potential namespace package. As such, Python keeps searching for a "normal" package that defines `__init__.py` for the same package name. Since Astroid has one and seemingly has it on `sys.path` in such a way as to get discovered, it gets selected as the package to use for the name `test`.

If you add a `tests/__init__.py` file then your issue will go away.
msg409812 - (view) Author: Daniël van Noord (danielnoord) * Date: 2022-01-05 23:30
Thanks Brett for that explanation!

Makes sense actually now that I think about it. I also wondered how astroid got onto my sys.path, but that's something for another time :)
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90399
2022-01-05 23:30:26danielnoordsetmessages: + msg409812
2022-01-05 23:10:20brett.cannonsetstatus: open -> closed
resolution: not a bug
messages: + msg409809

stage: resolved
2022-01-03 12:39:43danielnoordsetcomponents: + Library (Lib)
2022-01-03 12:21:51danielnoordcreate