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 ...
随机推荐
- ubuntu添加开机启动
vim /etc/init.d/mytest #!/bin/sh echo "$(pwd) and $USER and $(whoami)" >> /root/temp ...
- 九度oj题目1008:最短路径问题
题目描述: 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. 输入: ...
- 线段树练习5(codevs 4927)
题目描述 Description 有n个数和5种操作 add a b c:把区间[a,b]内的所有数都增加c set a b c:把区间[a,b]内的所有数都设为c sum a b:查询区间[a,b] ...
- 隐藏video标签的下载按钮
问题: 使用video标签时,有些浏览器会显示视频的下载按钮,而这并不是我们需要的功能,必须想办法去掉. 解决方法: 使用下面的css可以达到隐藏下载按钮的效果,但是点击下载的位置,还是能出现开始下载 ...
- Ubuntu 安装PostgreSQL
安装最新版: sudo apt-get install postgresql 安装完成后,默认会: (1)创建名为"postgres"的Linux用户 (2)创建名为"p ...
- Jenkins持续部署-Windows环境持续部署探究1
目录 Jenkins持续部署-Windows环境持续部署探究1 目录 前言 目的 方案流程 技术实现 PowerShell FTP上传插件 环境变量插件 脚本执行 远程调用 升级服务 启动服务 总结 ...
- JDBC 数据库连接 Java操作数据库 jdbc快速入门
JDBC基本概念 Java DataBase Connectivity 数据库连接 java操作数据库 本质上(sun公司的程序员)定义的一套操作关系型数据库的规则 既接口 更新内容之前 代码 pa ...
- http://www.yiibai.com/java8/java8_temporaladjusters.html
原文:http://www.yiibai.com/java8/java8_temporaladjusters.html TemporalAdjuster 是做日期数学计算.例如,要获得“本月第二个星期 ...
- 批量修改WORD表格属性
有时候需要对word中很多表格的属性进行修改,而word无法批量修改属性,所有这里记录一个宏 Sub TableFormatter() Dim oTbl As Table, i As Integer ...
- android 打开浏览器指定网页
<?xml version="1.0" encoding="utf-8"?> <!-- 定义当前布局的基本LinearLayout --> ...