程序代码

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

=============下面这个解法更能体现问题所在,就是模块的路径问题

问题:

我不能确定我为什么得到这个错误:

  1. ************************************************** **************
  2. Traceback (most recent call last):
  3. File "my.py" , line 3 ,
    in ?
  4. urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
  5. TypeError: 'module' object
    is not callable
  6. **************************************************

源代码如下:

  1. import urlparse
  2. urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')

答复:

"TypeError: 'module' object is not callable"这个信息是说你试图把"urlparse"这个模块作为一个函数来调用,但它却无法调用。

urlparse这个模块包含urlparse 和 urlsplit等函数。我把urlsplit也拖了进来,它的名字和模块名不同。这个可能能帮助你发现问题。以下是调用它们的两种方法。

(1)

  1. >>> import urlparse
  2. >>> urlparse.urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
  3. ( 'http' , 'www.cwi.nl:80' ,
    '/%7Eguido/Python.html', '' ,
    '' , '' )
  4. >>> urlparse.urlsplit( 'http://www.cwi.nl:80/%7Eguido/Python.html')
  5. ( 'http' , 'www.cwi.nl:80' ,
    '/%7Eguido/Python.html', '' ,
    '' )
  6. >>>

(2)

  1. >>> from urlparse import urlparse, urlsplit
  2. >>> urlparse( 'http://www.cwi.nl:80/%7Eguido/Python.html')
  3. ( 'http' , 'www.cwi.nl:80' ,
    '/%7Eguido/Python.html', '' ,
    '' , '' )
  4. >>> urlsplit( 'http://www.cwi.nl:80/%7Eguido/Python.html')
  5. ( 'http' , 'www.cwi.nl:80' ,
    '/%7Eguido/Python.html', '' ,
    '' )
  6. >>>

方法1可能更适合你。

TypeError: 'module' object is not callable 原因分析的更多相关文章

  1. Python TypeError: 'module' object is not callable 原因分析

    今天尝试使用pprint进行输出,语句为 >>>import pprint >>>pprint(people) 结果报错,TypeError: 'module' o ...

  2. python 报错——Python TypeError: 'module' object is not callable 原因分析

    原因分析:Python导入模块的方法有两种: import module 和 from module import 区别是前者所有导入的东西使用时需加上模块名的限定,而后者则不需要 例: >&g ...

  3. 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   ...

  4. python -- TypeError: 'module' object is not callable

    文件: 代码: import pprintmessge = 'It was a bringht cold day in April,and the clocks were striking thrir ...

  5. PyCharm+selenium环境搭建报错:Traceback (most recent call last): TypeError: 'module' object is not callable

    环境搭建好后,代码如下: from selenium import webdriverdriver = webdriver.chrome()driver.get("http://www.ba ...

  6. python import 错误 TypeError: 'module' object is not callable

    python import 错误 TypeError: 'module' object is not callable 在这里,有 Person.py test.py; 在 test.py 里面 im ...

  7. pip安装pillow——死循环:[WinError5] & [TypeError:'module' object is not callable]

    1.这次本来要安装个pillow,记得以前装了的,怎么这次就不行了.然后,下意识的使用:pip3 install pillow. 发现报错: [TypeError:'module' object is ...

  8. pip install 报错 TypeError: 'module' object is not callable

    $ pip install filetype Traceback (most recent call last): File "/usr/local/bin/pip2", line ...

  9. django TypeError: 'module' object is not callable

    原因:导入模块时直接把模块当函数使用 from rest_framework import reverse #import reverse module @api_view(("GET&qu ...

随机推荐

  1. json的两种表示结构(对象和数组).。

    JSON有两种表示结构,对象和数组.对象结构以”{”大括号开始,以”}”大括号结束.中间部分由0或多个以”,”分隔的”key(关键字)/value(值)”对构成,关键字和值之间以”:”分隔,语法结构如 ...

  2. 高精度&&FFT

    ACM-高精度模板(综合篇) 时间:-- :: 阅读: 评论: 收藏: [点我收藏+] 标签:高精度 在这里,我们约定,能用int表示的数据视为单精度,否则为高精度.所有函数的设计均采用带返回值的形式 ...

  3. 自动化运维之shell引号和正则表达式(二)

    1 shell引号 1)反斜线\ 转译 echo * 显示当前目录中所有的文件列表 echo \* 显示*字符 换行 find / \ 换行输入多行命令 > -name "test.t ...

  4. redis2.3.7安装时出现undefined reference to `clock_gettime'

    (转自:http://blog.csdn.net/qq_28779503/article/details/54844988) undefined reference to `clock_gettime ...

  5. 【Luogu】P2522Problemb(莫比乌斯反演)

    题目链接 同Zip—Queries,但是用到容斥原理 设f(n,m)是(x,y)的对数,其中1<=x<=n,1<=y<=m 则有f(n,m)-f(a-1,n)-f(b-1,m) ...

  6. 【leetcode最短路】818. Race Car

    https://leetcode.com/problems/race-car/description/ 1. BFS剪枝 0<=current position<=2*target.为什么 ...

  7. 转载: GMM-HMM学习笔记

    转载地址:http://blog.csdn.net/davidie/article/details/46929269 最近几天钻研了语音处理中的GMM-HMM模型,阅读了一些技术博客和学术论文,总算是 ...

  8. Java的反射机制和动态代理

    介绍Java注解的时候,多次提到了Java的反射API.与javax.lang.model不同的是,通过反射API可以获取程序在运行时刻的内部结构.反射API中提供的动态代理也是非常强大的功能,可以原 ...

  9. jenkins异常解答

    1.安装插件时offline 需要更换插件管理中的升级URL   http://mirror.xmission.com/jenkins/updates/current/update-center.js ...

  10. uva 11235 RMQ范围最大值

    题目大意:给一个整数上升序列,对于一系列询问区间(i,j),回答这段区间出现次数最多值所出现的次数. 分析:一个上升序列,相同的值聚集在一起,把相同的值的区间看作一个整体,假设这样的整体有n个,把他们 ...