python成功导入模块却不能正常使用

在正常使用python代码的时候,我们99%都会导入模块。
有一种情况:导入模块在ide上显示没有问题,但在使用的时候就会出现问题

code如下:

A.py

import B

class A_Class:
    def __init__(self):
        print('a_class init the code')

    def testA(self):
        print('执行testA')

B.py

import A
class B_Class:
    def __init__(self):
        print('b_class init the code')

    def testB(self):
        A.A_Class()
        print('执行testB')

b = B_Class()
b.testB()

如果我直接运行B模块,那么会出现如下报错信息:

b_class init the code
Traceback (most recent call last):
  File "/home/liudong/Desktop/test/B.py", line 1, in <module>
    import A
  File "/home/liudong/Desktop/test/A.py", line 1, in <module>
    import B
  File "/home/liudong/Desktop/test/B.py", line 11, in <module>
    b.testB()
  File "/home/liudong/Desktop/test/B.py", line 7, in testB
    A.A_Class()
AttributeError: module 'A' has no attribute 'A_Class'

这个问题的产生在于A.py文件,其中A文件中导入了B模块,如此产生了循环依赖,导致python代码出现问题,我们只需要将 A.py中的导入语句注释掉即可成功运行。
在这里插入图片描述

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值