Skip to main content
 首页 » 编程设计

python import * 或其他级别的列表

2025年04月02日9jpfss

<分区>

我正在尝试从另一个级别的模块中导入一些类。我可以输入所有类,但我尝试动态地输入

如果我这样做:

from ..previous_level.module import * 
    raise: SyntaxError: import * only allowed at module level 

同样来自 myapp 文件夹:

from myapp.previous_level.module import * 
    raise: SyntaxError: import * only allowed at module level 

所以我想:

my_classes = ['Class_Foo', 'Class_Bar'] 
for i in my_classes: 
    from ..previous_level.module import i 
        raise: ImportError: cannot import name 'i' 

还有:

my_classes = ['Class_Foo', 'Class_Bar'] 
for i in my_classes: 
    __import__('myapp').previous_level.module.y 
    raise: AttributeError: module 'myapp.previous_level.module' has no attribute 'y' 

我试过 string format , getattr() , __getattr__ 但没有成功。

这样导入是不可能的,还是我哪里做错了?