简体   繁体   English

Python模块互相导入

[英]Python modules importing each other

I have 2 modules importing each other, I try to nick a global variable from one of them into the other: 我有2个模块互相导入,我尝试将一个全局变量从其中一个切入另一个:

package1.module_1: package1.module_1:

import package1.module_2 as module_2

SOME_VARIABLE = 5

package1.module_2: package1.module_2:

import package1.module_1 as module_1

SOME_VARIABLE = module_1.SOME_VARIABLE 

It gives the error below: 它给出以下错误:

AttributeError: 'module' object has no attribute 'module_2'

How can I fix it? 我该如何解决?

Answer: don't use as . 答:不要as

instead of: 代替:

import package1.module_2 as module_2

use: 采用:

import package1.module_2 

the right syntax is 正确的语法是

from package1 import module_1

and

from package1 import module_2

but will led to circular import 但会导致循环进口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM