ModulenotFoundError with ementlib.import_module for subsodule
(我的英语真的很遗憾)
我写了一个OOP代码,我想要一个主文件,该文件(使用Ementlib自动)某些“模板”执行。每个模板都有一个“主文件”和其他对象,以实现良好的方案。但是我无法将我的“主模块” BeaCause Ementlib直接在开始代码的导入对象上(如下示例)。 请帮忙!我尝试使用Inspect Lib和Modulefinder Lib解决,或者在用OS.listdir解析文件名时,但是...没有...我完全被阻止了。
--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
...
输出终端,
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'
如果您可以帮助我,请提前感谢您(再次为我的英语^^感到抱歉)
(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 ^^)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在运行
main.py
,该导入beer.py
试图导入livraison.py
。问题是
beer.py
中的行,其中说:来自Livraison Import Livraison
。 Python试图根据main.py.py
的位置导入livraison
- 因此,就像您从Main中的Livraison Import Import Import Import Import
中一样.py 。与main.py.py
同一文件夹中没有livraison.py
,因此存在错误。要根据
beer.py
的位置导入,请使用:在
beer.py
中。。
表示Python Importslivraison
基于beer.py.py
的位置You are running
main.py
, which importsBeer.py
, which tries to importLivraison.py
.The problem is the line in
Beer.py
which says:from Livraison import Livraison
. Python tries to importLivraison
based on the position ofmain.py
- so it is like you wrotefrom Livraison import Livraison
inmain.py
. There is noLivraison.py
in the same folder asmain.py
, so there is an error.To import based on the position of
Beer.py
, use:in
Beer.py
.The
.
means that python importsLivraison
based on the position onBeer.py