TypeError: 'module' object is not callable 原因分析
程序代码
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可能更适合你。
TypeError: 'module' object is not callable 原因分析的更多相关文章
- Python TypeError: 'module' object is not callable 原因分析
今天尝试使用pprint进行输出,语句为 >>>import pprint >>>pprint(people) 结果报错,TypeError: 'module' o ...
- python 报错——Python TypeError: 'module' object is not callable 原因分析
原因分析:Python导入模块的方法有两种: import module 和 from module import 区别是前者所有导入的东西使用时需加上模块名的限定,而后者则不需要 例: >&g ...
- TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133
程序代码 class Person: #constructor def __init__(self,name,sex): self.Name = name ...
- python -- TypeError: 'module' object is not callable
文件: 代码: import pprintmessge = 'It was a bringht cold day in April,and the clocks were striking thrir ...
- PyCharm+selenium环境搭建报错:Traceback (most recent call last): TypeError: 'module' object is not callable
环境搭建好后,代码如下: from selenium import webdriverdriver = webdriver.chrome()driver.get("http://www.ba ...
- python import 错误 TypeError: 'module' object is not callable
python import 错误 TypeError: 'module' object is not callable 在这里,有 Person.py test.py; 在 test.py 里面 im ...
- pip安装pillow——死循环:[WinError5] & [TypeError:'module' object is not callable]
1.这次本来要安装个pillow,记得以前装了的,怎么这次就不行了.然后,下意识的使用:pip3 install pillow. 发现报错: [TypeError:'module' object is ...
- pip install 报错 TypeError: 'module' object is not callable
$ pip install filetype Traceback (most recent call last): File "/usr/local/bin/pip2", line ...
- django TypeError: 'module' object is not callable
原因:导入模块时直接把模块当函数使用 from rest_framework import reverse #import reverse module @api_view(("GET&qu ...
随机推荐
- .NET重构(六):删除用户和结账的理解
导读:这是第二回机房了,第一回不明不白,不清不楚的就过去了(相对),这一回,有了新的发现.就是在用户删除的时候,涉及到的一些逻辑问题,以及结账时的数据来源问题. 一.用户删除 问题:第一次机房,包括重 ...
- 九度oj 题目1528:最长回文子串
题目描述: 回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串. 回文子串,顾名思义,即字符串中满足回文性质的子串. 给出一个只由小写英文字符a,b,c...x, ...
- BZOJ-1303 中位数图
先找到B的位置x,然后依次统计A[i..x-1](0<i<x)中小于B的个数,和A[x+1..i](x<i<n)中大于B的个数 最后Answer等于(左边有i个小于B的情况总数 ...
- Linux(7):用户管理
用户管理 让一个脚本或命令开机自启动的方法: # 方法一: 把脚本放到 /etc/rc.local 中 # 方法二: 把脚本或命令通过 chkconfig 管理 # 如何让一个脚本被 chkconfi ...
- elasticsearch入门使用(五) kibana&x-pack安装使用
Kibana User Guide 一.UI安装 https://www.elastic.co/downloads/kibana 下载rpm直接运行即可 二.参数配置 find / -name kib ...
- python练习之-计算器
学习以堆栈模式编写-计算器 堆栈特点:先进后出, 如下: #!/opt/python3/bin/python3 # Author: yong import re def is_symbol(eleme ...
- Linux下多线程编程-信号量
今天来谈谈线程的同步--信号量. 首先来看看一些概念性的东西: 如进程.线程同步,可理解为进程或线程A和B一块配合,A执行到一定程度时要依靠B的某个结果,于是停下来,示意B运行:B依言执行,再将结果给 ...
- 深入GCD(三): Dispatch Sources
何为Dispatch Sources简单来说,dispatch source是一个监视某些类型事件的对象.当这些事件发生时,它自动将一个block放入一个dispatch queue的执行例程中.说的 ...
- 【spring data jpa】报错如下:Caused by: javax.persistence.EntityNotFoundException: Unable to find com.rollong.chinatower.server.persistence.entity.staff.Department with id 0
报错如下: org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.rollong.chi ...
- python解析网页中js动态添加的内容
https://www.cnblogs.com/asmblog/archive/2013/05/07/3063809.html https://www.zhihu.com/question/21471 ...