Python Forum
Understanding Python's Import Engine
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding Python's Import Engine
#1
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?
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,255 May-10-2023, 02:13 PM
Last Post: Calab
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,740 May-09-2023, 08:47 AM
Last Post: buran
  Understanding Python classes PythonNewbee 3 1,182 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  Understanding Python super() for classes OmegaRed94 1 1,826 Jun-09-2021, 09:02 AM
Last Post: buran
  Search Engine nman52 1 53,665 Dec-16-2020, 11:46 AM
Last Post: Larz60+
  problem python search engine forumpy 0 1,726 Sep-15-2020, 10:26 PM
Last Post: forumpy
  Better Understanding Of Object Orientation In Python JoeDainton123 3 2,462 Aug-30-2020, 02:49 PM
Last Post: deanhystad
  Voynich search engine in python using dashes & dot totals to find Italian words Pleiades 3 3,496 Oct-10-2019, 10:04 PM
Last Post: Pleiades
  Making an application with a 3d engine in it moo5e 1 2,110 May-27-2019, 03:17 PM
Last Post: heiner55
  Help with understanding a python package pyhill00 4 3,019 Mar-21-2019, 12:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020