程序代码

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. 划分树C++版百度百科模板

    此代码同hdu2665改编#include <iostream> #include <cstdio> #include<cstring> #include < ...

  2. 防火墙iptables介绍

    防火墙: netfilter/iptables是集成在Linux2.4.X版本内核中的包过滤防火墙系统.该架构可以实现数据包过滤,网络地址转换以及数据包管理功能.linux中防火墙分为两部分:netf ...

  3. BZOJ 2829 信用卡凸包 ——计算几何

    凸包裸题 #include <map> #include <cmath> #include <queue> #include <cstdio> #inc ...

  4. *AtCoder Regular Contest 096E - Everything on It

    $n \leq 3000$个酱,丢进拉面里,需要没两碗面的酱一样,并且每个酱至少出现两次,面的数量随意.问方案数.对一给定质数取模. 没法dp就大力容斥辣.. $Ans=\sum_{i=0}^n (- ...

  5. ADO:防止更新的数据含有单引号而出错

    原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] public void Update( string au_lname, string zip,string au ...

  6. hdu 4091 Zombie’s Treasure Chest 贪心+枚举

    转自:http://blog.csdn.net/a601025382s/article/details/12308193 题意: 输入背包体积n,绿宝石体积s1,价值v1,蓝宝石体积s2,价值v2,宝 ...

  7. hdu3078 建层次树+在线LCA算法+排序

    题意:n个点,n-1条边构成无向树,每个节点有权,Q次询问,每次或问从a->b的最短路中,权第k大的值,/或者更新节点a的权, 思路:在线LCA,先dfs生成树0,标记出层数和fa[](每个节点 ...

  8. ubuntu16.04安装mysql5.7.15

    1.在官网下载mysql安装包 直接选择第一个下载好了就行 2.进入你的下载文件夹下面 键入命令: tar -xvf mysql-server_5.7.13-1ubuntu16.04_i386.deb ...

  9. Linux下的搜索命令grep(转)

    一.简介 grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具, ...

  10. js可以控制文件上传的速度吗?

    为了减轻服务器负载,对于上传和下载的情况,我们需要进行流量控制,一般的方法是服务端做限流举措,比如很多ftp服务器,但是我想是不是可以使用前端js做呢? 顺着这个想法,我查了下资料,目前来看结论是No ...