Python Forum
Understanding Python's Import Engine - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Understanding Python's Import Engine (/thread-24302.html)



Understanding Python's Import Engine - MysticaL - Feb-07-2020

Hi,

I have a question pertaining to importing post python 3.4 (I am using python 3.8). It is my understanding that the import engine will internally invoke meta path finders in sys.meta_path, of which the third PathFinder is used to find modules using sys.path. Given the following file structure, with "test" and "a" being namespace packages,

example/test/a/b.py

If I run an interpreter with cwd as example, the following code fails:
>>> import test.a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'test.a'


My question is, since the test builtin does not have a module "a", why does the third PathFinder fail to find the module, as the FileFinder with cwd should be able to locate the module?


RE: Understanding Python's Import Engine - snippsat - Feb-07-2020

example will be the package_name at top level,so you most import from example.
Let say b.py is:
def foo():
    return 42
Import test.
E:\div_code
λ ptpython
>>> import example.test.a.b
>>> example.test.a.b.foo()
42

>>> # Using from
>>> from example.test.a import b
>>> b.foo()
42
For this to work most example folder with sub-folder be in sys.path of Python version used eg i use Python 3.7 here.