Reputation: 21
(my english is really bad sorry)
I write a OOP code and I want a main file who import (auto with importlib) some "templates" to execute. Every templates have a "main file" and others objects for a good oriented programmation. BUT I can't import my "main module" beacause importlib raise ModuleNotFoundError directly on import object at the beginning code (example below). Please help ! I try to resolve with inspect lib and ModuleFinder lib or force at parsing my file name in os.listdir but... nothing... I'm totally blocked.
--main.py
--templates/
---__init__.py
---Beer/
----__init__.py
----Beer.py
----Livraison.py
main.py
...
def import_templates(package, recursive=True):
if isinstance(package, str):
print(package)
package = import_module(package)
print('1 : ' + str(package))
results = {}
for loader, name, is_pkg in walk_packages(package.__path__):
full_name = package.__name__ + '.' + name
results[name] = import_module(full_name)
if recursive and is_pkg:
results.update(import_templates(full_name))
return results
...
Beer.py
...
from Livraison import Livraison
...
output terminal
templates
1 : <module 'templates' from 'PATH/GestionEmbed/templates/__init__.py'>
templates.Beer
1 : <module 'templates.Beer' from 'PATH/GestionEmbed/templates/Beer/__init__.py'>
Traceback (most recent call last):
File "PATH/GestionEmbed/main.py", line 59, in <module>
tmp = import_templates("templates")
File "PATH/GestionEmbed/main.py", line 45, in import_templates
results.update(import_templates(full_name))
File "PATH/GestionEmbed/main.py", line 43, in import_templates
results[name] = import_module(full_name)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "PATH/GestionEmbed/templates/Beer/Beer.py", line 7, in <module>
from Livraison import Livraison
ModuleNotFoundError: No module named 'Livraison'
Thank you in advance if you can help me (and again sorry for my english ^^)
Upvotes: 2
Views: 8055
Reputation: 695
You are running main.py
, which imports Beer.py
, which tries to import Livraison.py
.
The problem is the line in Beer.py
which says: from Livraison import Livraison
. Python tries to import Livraison
based on the position of main.py
- so it is like you wrote from Livraison import Livraison
in main.py
. There is no Livraison.py
in the same folder as main.py
, so there is an error.
To import based on the position of Beer.py
, use:
from .Livraison import Livraison
in Beer.py
.
The .
means that python imports Livraison
based on the position on Beer.py
Upvotes: 1