Python 3.5.1 urllib has no attribute request

。_饼干妹妹 提交于 2019-12-23 06:56:06

问题


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

I am wondering where is urlopen, or is my python module pointing to the wrong file?


回答1:


Use this:

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.

According to this




回答2:


In python 3.6.x this worked for me, this way I did not have to change the code at all:

import urllib.request as urllib



回答3:


If nothing above worked for you, try renaming your python module.

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'

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




回答4:


import urllib.request as urllib

then in the code call urlopen function

example:

test = urllib.urlopen("----")


来源:https://stackoverflow.com/questions/37042152/python-3-5-1-urllib-has-no-attribute-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!