python错误TypeError: 'module' object is not callable 的解决方法

本文解决了一个在Python中使用urlparse模块时遇到的TypeError错误问题,提供了正确的调用方法,并推荐了深入学习Python模块章节的教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://blog.youkuaiyun.com/huzhenwei/article/details/2895909

问题:


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

  1. ****************************************************************
  2. Traceback(mostrecentcalllast):
  3. File"my.py",line3,in?
  4. urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
  5. TypeError:'module'objectisnotcallable
  6. **************************************************

源代码如下:

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

答复:

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

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

(1)

  1. >>>importurlparse
  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. >>>fromurlparseimporturlparse,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可能更适合你。

我建议你阅读指南里的模块章节:http://docs.python.org/tut/node8.html及所以你没读过的前面的章节。

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------

原文如下:

dear python users

I am not sure why I am getting

************************************************** **************
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
************************************************** **************

with this code

************************************************** **************
import urlparse

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


thank you



Gary Wessle

reply:

The message "TypeError: 'module' object is not callable" means that the
"urlparse" that you are trying to call as a function is a module and is
thus not callable.

The module urlparse contains functions urlparse and urlsplit, among
others. I'm dragging urlsplit into the arena as its name is not the same
as the module name, and it might help you see what is happening. There
are two ways of calling them:

(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', '', '')
>>>


Method (1) is probably better for you at the moment.

I suggest that you read the Modules section in the tutorial:
http://docs.python.org/tut/node8.html

*and* all the earlier sections if you haven't already.

John Machin


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值