我在使用Python的导入时遇到了问题。我编写了一个已完成的模块,它本身使用了几个子模块(这些都是被导入的)。
例如:
module:
main_class.py
submodule1.py
....
现在我想通过另一个超级模块使用此已完成的模块,因此文件夹结构将会改变,如下所示。
supermodule:
main_class_super.py -- this class imports module.main_class
module:
main_class.py
submodule1.py
....
然而,现在在模块内部使用的所有导入代码在main_class.py
中都失败了(我猜这是因为导入现在在main_class_super.py
的命名空间中工作)。
有没有什么办法可以解决这个问题,而不必重新构建整个源代码?
具体的错误信息:
在我的main_class.py
中,我使用了以下代码:
import submodule1
在我的
supermodule.py
文件中,我使用了以下代码行:import module.main_class
执行导入
module.main_class
的超类时,当然也会执行import submodule1
行,但由于它无法在supermodule.py
的命名空间中找到该模块而失败。