700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python import 错误 TypeError: 'module' object is not callable

python import 错误 TypeError: 'module' object is not callable

时间:2021-06-01 12:11:41

相关推荐

python import 错误 TypeError: 'module' object is not callable

python import 错误 TypeError: 'module' object is not callable

在这里,有 Person.py test.py; 在 test.py 里面 import Person 总是调用方法出错

Person.py

class Person:def __init__(self,name):self.name = nameprint('this name is ',name)def hello(self):print('hello python')

出现错误的 test.py

import Personperson = Person('dd')person.hello()

错误原因:原来是 import Person 表示的是 导入 Person文件里面的所有东西,调用方法,还要继续再加一层 比如,Person.Person('kk')

解决方案一:

import Personperson = Person.Person('Tom')person.hello()

解决方案二:

from Person import *person = Person('Tom')person.hello()

打印结果都是:

this name is Tomhello python

参考:/huzhenwei/article/details/2895909

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。