Python学习篇 之 error

来源:互联网 发布:开淘宝店怎么找厂家 编辑:程序博客网 时间:2024/05/20 22:01

1 No moduled named ...

使用Pycharm ,在一个模块中导入另一个模块的时,出现“No module named ...”的错误

解决方法:找到目标文件所在的文件夹,将其标记为source root



2 TypeError: 'module' object is not callable 

有Person.py模块如下:
class Person:    def __init__(self,name='未知',sex='未知',age='未知'):        self.name = name;        self.sex = sex;        self.age = age;    def introduceMyself(self):        print('My name is ',self.name,". I'm ",self.age,'years old now.')

在Test.py模块中使用Person模块中的Person类:
import Personperson = Person("MrZhang","男",22)person.introduceMyself();

出现错误:

TypeError: 'module' object is not callable

原因:
Python导入模块的方法有两种,import module和from module import*,区别是前者导入的东西使用时需要加上模块名进行限定,而后者不需要,直接使用即可。
import Personperson = Person.Person("MrZhang","男",22)person.introduceMyself();

from Person import*person = Person("MrZhang","男",22)person.introduceMyself();