ModulenotFoundError with ementlib.import_module for subsodule

发布于 2025-01-20 03:19:30 字数 2352 浏览 4 评论 0原文

(我的英语真的很遗憾)

我写了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2025-01-27 03:19:31

您正在运行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的位置导入,请使用:

from .Livraison import Livraison

beer.py中。

表示Python Imports livraison基于beer.py.py的位置

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文