Python AttributeError: 模块 'urllib' 没有属性 'parse'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41501638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: module 'urllib' has no attribute 'parse'
提问by Hong Yinjie
python 3.5.2
蟒蛇 3.5.2
code 1
代码 1
import urllib
s = urllib.parse.quote('"')
print(s)
it gave this error:
它给出了这个错误:
AttributeError: module 'urllib' has no attribute 'parse'
AttributeError: 模块 'urllib' 没有属性 'parse'
code 2
代码 2
from urllib.parse import quote
# import urllib
# s = urllib.parse.quote('"')
s = quote('"')
print(s)
it works...
有用...
code3
代码3
from flask import Flask
# from urllib.parse import quote
# s = quote('"')
import urllib
s = urllib.parse.quote('"')
print(s)
it works,too. because of flask?
它也有效。因为烧瓶?
Why I don't have the error anymore? is it a bug ?
为什么我没有错误了?这是一个错误吗?
回答by Nizam Mohamed
The urllib
package serves as a namespace only. There are other modules under urllib
like request
and parse
.
For optimization importing urllib
doesn't import other modules under it. Because doing so would consume processor cycles and memory, but people may not need those other modules.
Individual modules under urllib
must be imported separately depending on the needs.
该urllib
包仅用作命名空间。urllib
likerequest
和下还有其他模块parse
。
为了优化导入urllib
不会导入其下的其他模块。因为这样做会消耗处理器周期和内存,但人们可能不需要那些其他模块。
下面的各个模块urllib
必须根据需要单独导入。
Try these, the first one fails but the second succeeds because when flask
is imported flask
itself imports urllib.parse
.
尝试这些,第一个失败但第二个成功,因为何时flask
导入flask
本身会导入urllib.parse
。
python3 -c 'import urllib, sys;print(sys.modules["urllib.parse"])'
python3 -c 'import flask, sys;print(sys.modules["urllib.parse"])'
回答by Ivan Georgiev
For code 1to work you need to import the urllib.parse
module, not the function quote
. This way you can refer to the quote
function with full module qualifier. With this approach you can use any function defined in the parse
module:
要使代码 1工作,您需要导入urllib.parse
模块,而不是函数quote
。这样你就可以quote
用完整的模块限定符来引用函数。通过这种方法,您可以使用parse
模块中定义的任何函数:
import urllib.parse
s = urllib.parse.quote('"')
print(s)
code 2works, because you import only the parse
function and refer to it without module qualifier, since it is not imported in the context of the module. With this approach you can use only the explicitly imported function from the parse
module.
代码 2有效,因为您只导入parse
函数并在没有模块限定符的情况下引用它,因为它没有在模块的上下文中导入。通过这种方法,您只能使用parse
模块中显式导入的函数。
code 3works, because flask
imports implicitly the urllib.parse
module. The parse
module becomes available in the urllib
module context. Once you import urllib
, urllib.parse
becomes readily available and you can use it as in code 1
代码 3有效,因为flask
隐式导入urllib.parse
模块。该parse
模块在urllib
模块上下文中变得可用。导入后urllib
,urllib.parse
就可以使用了,您可以像在代码 1 中一样使用它
回答by Abu Shoeb
I encountered a similar issue and realized it's a version problem. So here is my solution in case it helps anyone:
我遇到了类似的问题,并意识到这是一个版本问题。所以这是我的解决方案,以防它帮助任何人:
# For Python 2.X
urllib.quote_plus(query)
# For Python 3.x
urllib.parse.quote_plus(query)
Reference https://github.com/schollz/playlistfromsong/issues/36