Python 3.5.1 urllib 没有属性请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37042152/
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
Python 3.5.1 urllib has no attribute request
提问by user1999806
I have tried
我试过了
import urllib.request
or
或者
import urllib
The path for my urllib is
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/__init__.py
我的 urllib 的路径是
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/__init__.py
I am wondering where is urlopen, or is my python module pointing to the wrong file?
我想知道 urlopen 在哪里,或者我的 python 模块指向错误的文件?
回答by Swordsman
Use this:
用这个:
import urllib.request
import urllib.request
The reason is:
原因是:
With packages, like this, you sometimes need to explicitly import the piece you want. That way, the urllib module doesn't have to load everything up just because you wanted one small part.
对于像这样的包,您有时需要显式导入您想要的部分。这样,urllib 模块就不必仅仅因为您想要一小部分就加载所有内容。
According to this
根据这个
回答by MP?kalski
In python 3.6.x this worked for me, this way I did not have to change the code at all:
在 python 3.6.x 这对我有用,这样我就不必更改代码了:
import urllib.request as urllib
回答by Kashif
Use this way
用这种方式
import urllib.request
urllib.request.urlopen('http://216.58.192.142',timeout=1)
回答by JohnnyFun
If nothing above worked for you, try renaming your python module.
如果以上都不适合您,请尝试重命名您的 python 模块。
In my particular case, the issue was that the file I was running was called http.py
. Once I changed the name to test-http.py
, importing urllib.request resolved the error AttributeError: module 'urllib' has no attribute 'request'
在我的特殊情况下,问题是我正在运行的文件名为http.py
. 一旦我将名称更改为test-http.py
,导入 urllib.request 就解决了错误AttributeError: module 'urllib' has no attribute 'request'
I had noticed that further up the exception trace the internal packages were trying to fetch a module called http
, so guessing my module's name was wonkin stuff up...
我注意到内部包在异常跟踪的http
更深处试图获取一个名为 的模块,所以猜测我的模块的名称是wonkin 的东西......
回答by Hassan Saeed
import urllib.request as urllib
then in the code call urlopen function
然后在代码中调用 urlopen 函数
example:
例子:
test = urllib.urlopen("----")