程序代码

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. ajax 下拉加载更多效果

    1.生成HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  2. 暑假训练Round1——G: Hkhv的水题之二(字符串的最小表示)

    Problem 1057: Hkhv的水题之二 Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  3. 刷题总结——road(ssoi)

    题目: 题目背景 SOURCE:NOIP2016-RZZ-1 题目描述 有 N 个城市,这些城市通过 M 条无向边互相连通,每条边有一个权值 Ci ,表示这条边的长度为 2^(Ci) ,没有两条边的长 ...

  4. 【CCF】路径压缩 区间dp

    [题意] 改编哈夫曼树,限制从左到右字母的编码按字典序递增 [思路] 因为是二进制编码,所以是二叉树: 因为是前缀码,所以每个字母都是叶子结点,不可能是内结点: 因为要按字典序递增,所以只能是相邻的结 ...

  5. javaScriptCore 实战与小结

      源码在这,看不懂的直接撸源码就行,转载声明出处 原生调用JS的大致流程,做了个思维简图 这是代码流程 // JS数据 func getJSVar() { let context: JSContex ...

  6. Biorhythms(poj 1006)

    Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰 ...

  7. DP的序--Codeforces956E. Wardrobe

    $n \leq 10000$个盒子,有高度,高度总和$\leq 10000$,盒子有重要的和不重要的,问最多有多少重要盒子的底端在区间$[L,R]$. 这是个入门级的DP,但需要一点胆量MD这题能放D ...

  8. spring-基于注解的配置

    基于注解的配置 除了采用采用xml来配置bean之外,也可以采用注解的方式来定义,注册,加载bean. 注解的方式在spring中默认时不开启的,所以需要在xml文件中进行配置启用 注解的启动方式有下 ...

  9. eopkg命令

    #命令: add-repo (ar)  ---添加存储库 blame (bl)  ---包所有者和发布信息 build (bi)  ---建立eopkg包 check  ---验证安装 clean  ...

  10. Maven插件开发教程收集(待实践)

    官方教程:http://maven.apache.org/plugin-developers/index.html http://blog.csdn.net/csfreebird/article/de ...