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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:05:44  来源:igfitidea点击:

AttributeError: module 'urllib' has no attribute 'parse'

pythonurllibpython-3.5

提问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 urllibpackage serves as a namespace only. There are other modules under urlliblike requestand parse.
For optimization importing urllibdoesn'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 urllibmust be imported separately depending on the needs.

urllib包仅用作命名空间。urlliblikerequest和下还有其他模块parse
为了优化导入urllib不会导入其下的其他模块。因为这样做会消耗处理器周期和内存,但人们可能不需要那些其他模块。
下面的各个模块urllib必须根据需要单独导入。

Try these, the first one fails but the second succeeds because when flaskis imported flaskitself 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.parsemodule, not the function quote. This way you can refer to the quotefunction with full module qualifier. With this approach you can use any function defined in the parsemodule:

要使代码 1工作,您需要导入urllib.parse模块,而不是函数quote。这样你就可以quote用完整的模块限定符来引用函数。通过这种方法,您可以使用parse模块中定义的任何函数:

import urllib.parse
s = urllib.parse.quote('"')
print(s)

code 2works, because you import only the parsefunction 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 parsemodule.

代码 2有效,因为您只导入parse函数并在没有模块限定符的情况下引用它,因为它没有在模块的上下文中导入。通过这种方法,您只能使用parse模块中显式导入的函数。

code 3works, because flaskimports implicitly the urllib.parsemodule. The parsemodule becomes available in the urllibmodule context. Once you import urllib, urllib.parsebecomes readily available and you can use it as in code 1

代码 3有效,因为flask隐式导入urllib.parse模块。该parse模块在urllib模块上下文中变得可用。导入后urlliburllib.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

参考https://github.com/schollz/playlistfromsong/issues/36