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 ...
随机推荐
- 2013 Asia acm Hangzhou Regional Contest 杭州现场赛
B Stealing Harry Potter's Precious 题目大意:给定一个n*m的地图,某些点可以走,某些点可以走某些点不可以走,给定一个起点,又给出了k个点k<=4,要求从起点 ...
- uva 11916 解模方程a^x=b (mod n)
Emoogle Grid You have to color an M x N ( 1M, N108) two dimensional grid. You will be provided K ...
- 安卓解析XML文件
安卓解析XML文件 主要有三种方式:DOM解析.SAX解析.PULL解析 其中: DOM解析为等XMl文件全部加载后,然后根据需要解析的内容解析出所需的内容数据. SAX解析为从XML文件中执行一行, ...
- Redis数据结构之跳跃表
跳跃表是一种有序数据结构,它通过在每个节点中维持多个指向其他节点的指针,从而达到快速访问节点的目的. 一.跳跃表结构定义1. 跳跃表节点结构定义: 2. 跳跃表结构定义: 示例: 二.跳跃表节点中各种 ...
- WEB学习-CSS盒模型
盒子的区域 一个盒子中主要的属性就5个:width.height.padding.border.margin. width是“宽度”的意思,CSS中width指的是内容的宽度,而不是盒子的宽度. he ...
- Croc Champ 2013 - Round 2 C. Cube Problem
问满足a^3 + b^3 + c^3 + n = (a+b+c)^3 的 (a,b,c)的个数 可化简为 n = 3*(a + b) (a + c) (b + c) 于是 n / 3 = (a + b ...
- 解决本地调用office组件成功,但是发布到IIS中出现的错误(检索COM类工厂中CLSID为{00024500-0000-0000-C000-000000000046}的组件时失败)
在C#操作word或者Excel,我们可能会用到微软内置的COM组件,会出现很多问题. 如:在本地调试导出Excel没有问题,发布到IIS就有问题了,检测到的异常: 我们会发现在iis上运行的程序,没 ...
- Windows下部署多个Tomcat
编辑bin/startup.bat SET JAVA_HOME=...(JDK所在路径) SET CATALINA_HOME=...(Tomcat解压的路径) 编辑server.xml文件 <! ...
- Jetty插件实现热部署(开发时修改文件自动重启Jetty)
在pom.xml文件中配置Jetty插件的参数:scanIntervalSeconds <plugin> <groupId>org.mortbay.jetty</grou ...
- Spring MVC集成Spring Data Reids和Spring Session实现Session共享出现:No bean named 'springSessionRepositoryFilter' available
出现这个问题:No bean named 'springSessionRepositoryFilter' available的的原因: 1.Spring MVC加载了多个配置文件导致的,并不是版本问题 ...