如何从用Ementlib导入的软件包执行模块

发布于 2025-01-31 21:26:08 字数 1362 浏览 3 评论 0原文

我在一个称为testpackageb模块的软件包中有一个模块,

testb.py带有内容:

__all__ = ['testb']


def testb():
    print("Hello World from Test B in testpackageB")
    

__ init __ init __. py file the Package Directory包含

from testpackageB import testb

:动态导入该模块的代码如下:

import importlib


if __name__ == "__main__":
    tt = importlib.import_module(".testb", "testpackageB")

    print(type(tt))  # Shows: <class 'module'>

    # All of the following do not work:
    #  tt["testb"]()
    #  tt()
    #  tt.testb()

我不理解和找不到的是一个简单的答案:

我如何在此主要功能中执行testpackageb.testb?


评论我了解到tt.testb()正在工作。

但这不是您想要的。因为“ testb”是动态的,因此您在变量中具有该名称。

请参阅此代码:(略微更改)

    module = "testb"
    package = "testpackageB"
    tt = importlib.import_module(f".{module}", package)

    tt["module"]().  # Gives error: TypeError: 'module' object is not subscriptable 

,因此,我重新介绍了一个问题:

如何在此主要功能中执行testpackageb.testb,

使用testpackagebtestb在变量中?

I have a module in a package called testpackageB

module is called testb.py with contents:

__all__ = ['testb']


def testb():
    print("Hello World from Test B in testpackageB")
    

The __init__.py file the package directory contains:

from testpackageB import testb

The code to import this module dynamically is as follows:

import importlib


if __name__ == "__main__":
    tt = importlib.import_module(".testb", "testpackageB")

    print(type(tt))  # Shows: <class 'module'>

    # All of the following do not work:
    #  tt["testb"]()
    #  tt()
    #  tt.testb()

What I don't understand and can't find is a simple answer on the question:

How do I execute testpackageB.testb in this main function?


After getting comments I learned that tt.testb() is working.

But that is not the one you want. Because 'testb' is dynamic, so you have that name in a variable.

See this code: (slightly changed)

    module = "testb"
    package = "testpackageB"
    tt = importlib.import_module(f".{module}", package)

    tt["module"]().  # Gives error: TypeError: 'module' object is not subscriptable 

So, I rephrase the question:

How do I execute testpackageB.testb in this main function,

with testpackageB and testb in variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

鸠书 2025-02-07 21:26:08

我找到了解决方案:

如何执行动态导入的软件包 - 模块功能

文件夹结构:

- testpackageB
   - testb.py
       def testb(): ....

- main.py

testpackageb/testb.py

__all__ = ['testb']

def testb():
    print("Hello World from testb in testpackageB")
    

testpackageb/ init .py

from testpackageB import testb

main.py.py

import importlib

if __name__ == "__main__":
   
    package = "testpackageB"
    module = "testb"
    function = "testb"
    tt = importlib.import_module(f".{module}", package)

    func = getattr(tt, function)
    if func:
        func()

I have found the solution:

How to execute a dynamically imported package-module-function

Folder structure:

- testpackageB
   - testb.py
       def testb(): ....

- main.py

testpackageB/testb.py

__all__ = ['testb']

def testb():
    print("Hello World from testb in testpackageB")
    

testpackageB/init.py

from testpackageB import testb

main.py

import importlib

if __name__ == "__main__":
   
    package = "testpackageB"
    module = "testb"
    function = "testb"
    tt = importlib.import_module(f".{module}", package)

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