【Python】TypeError: 'module' object is not callable 原因分析
2013-02-15 13:22
881 查看
程序代码
class Person:
#constructor
def __init__(self,name,sex):
self.Name = name
self.Sex = sex
def ToString(self):
return 'Name:'+self.Name+',Sex:'+self.Sex
在IDLE中报错:
>>> import Person
>>> per = Person('dnawo','man')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
per = Person('dnawo','man')
TypeError: 'module' object is not callable
原因分析:
Python导入模块的方法有两种:import module 和 from module import,区别是前者所有导入的东西使用时需加上模块名的限定,而后者不要。
正确的代码:
>>> import Person
>>> person = Person.Person('dnawo','man')
>>> print person.Name
或
>>> from Person import *
>>> person = Person('dnawo','man')
>>> print person.Name
=============下面这个解法更能体现问题所在,就是模块的路径问题
问题:
我不能确定我为什么得到这个错误:
************************************************** **************
Traceback (most recent call last):
File "my.py" , line 3 , in ?
urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
TypeError: 'module' object is not callable
**************************************************
源代码如下:
import urlparse
urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
答复:
"TypeError: 'module' object is not callable"这个信息是说你试图把"urlparse"这个模块作为一个函数来调用,但它却无法调用。
urlparse这个模块包含urlparse 和 urlsplit等函数。我把urlsplit也拖了进来,它的名字和模块名不同。这个可能能帮助你发现问题。以下是调用它们的两种方法。
(1)
>>> import urlparse
>>> urlparse.urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
( 'http' , 'www.cwi.nl:80' , '/%7Eguido/Python.html', '' , '' , '' )
>>> urlparse.urlsplit( 'http://www.cwi.nl:80/%7Eguido/Python.html')
( 'http' , 'www.cwi.nl:80' , '/%7Eguido/Python.html', '' , '' )
>>>
(2)
>>> from urlparse import urlparse, urlsplit
>>> urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
( 'http' , 'www.cwi.nl:80' , '/%7Eguido/Python.html', '' , '' , '' )
>>> urlsplit( 'http://www.cwi.nl:80/%7Eguido/Python.html')
( 'http' , 'www.cwi.nl:80' , '/%7Eguido/Python.html', '' , '' )
>>>
方法1可能更适合你。
class Person:
#constructor
def __init__(self,name,sex):
self.Name = name
self.Sex = sex
def ToString(self):
return 'Name:'+self.Name+',Sex:'+self.Sex
在IDLE中报错:
>>> import Person
>>> per = Person('dnawo','man')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
per = Person('dnawo','man')
TypeError: 'module' object is not callable
原因分析:
Python导入模块的方法有两种:import module 和 from module import,区别是前者所有导入的东西使用时需加上模块名的限定,而后者不要。
正确的代码:
>>> import Person
>>> person = Person.Person('dnawo','man')
>>> print person.Name
或
>>> from Person import *
>>> person = Person('dnawo','man')
>>> print person.Name
=============下面这个解法更能体现问题所在,就是模块的路径问题
问题:
我不能确定我为什么得到这个错误:
************************************************** **************
Traceback (most recent call last):
File "my.py" , line 3 , in ?
urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
TypeError: 'module' object is not callable
**************************************************
源代码如下:
import urlparse
urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
答复:
"TypeError: 'module' object is not callable"这个信息是说你试图把"urlparse"这个模块作为一个函数来调用,但它却无法调用。
urlparse这个模块包含urlparse 和 urlsplit等函数。我把urlsplit也拖了进来,它的名字和模块名不同。这个可能能帮助你发现问题。以下是调用它们的两种方法。
(1)
>>> import urlparse
>>> urlparse.urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
( 'http' , 'www.cwi.nl:80' , '/%7Eguido/Python.html', '' , '' , '' )
>>> urlparse.urlsplit( 'http://www.cwi.nl:80/%7Eguido/Python.html')
( 'http' , 'www.cwi.nl:80' , '/%7Eguido/Python.html', '' , '' )
>>>
(2)
>>> from urlparse import urlparse, urlsplit
>>> urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
( 'http' , 'www.cwi.nl:80' , '/%7Eguido/Python.html', '' , '' , '' )
>>> urlsplit( 'http://www.cwi.nl:80/%7Eguido/Python.html')
( 'http' , 'www.cwi.nl:80' , '/%7Eguido/Python.html', '' , '' )
>>>
方法1可能更适合你。
相关文章推荐
- 【Python】TypeError: 'module' object is not callable 原因分析
- 【Python】TypeError: 'module' object is not callable 原因分析
- 【Python】TypeError: 'module' object is not callable 原因分析
- TypeError: 'module' object is not callable 原因分析(python模块导入注意事项)
- 【Python】TypeError: 'module' object is not callable 原因分析
- TypeError: 'module' object is not callable 原因分析
- TypeError: 'module' object is not callable 原因分析
- python 报错——Python TypeError: 'module' object is not callable 原因分析
- TypeError: 'module' object is not callable 原因分析
- TypeError: 'module' object is not callable 原因分析
- TypeError: 'module' object is not callable 原因分析
- Python导入模块(包)的两种方式 TypeError: 'module' object is not callable
- 【python系列】TypeError: 'module' object is not callable 原因分析
- Python TypeError: 'module' object is not callable 详解
- python错误TypeError: 'module' object is not callable 的解决方法
- TypeError: 'module' object is not callable 原因分析
- python错误TypeError: 'module' object is not callable 的解决方法
- python TypeError: 'module' object is not callable
- python错误:TypeError: 'module' object is not callable
- 解决"学习python-flask教程,下载源码运行的时候出现:TypeError: 'bool' object is not callable 错误代码:"