Python 3.5.1 urllib has no attribute request

后端 未结 6 1391
礼貌的吻别
礼貌的吻别 2020-12-17 08:19

I have tried

import urllib.request

or

import urllib

The path for my urllib is /Library/Framework

相关标签:
6条回答
  • 2020-12-17 08:30

    Use this way

    import urllib.request
    urllib.request.urlopen('http://216.58.192.142',timeout=1)
    
    0 讨论(0)
  • 2020-12-17 08:39

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

    0 讨论(0)
  • 2020-12-17 08:39

    I had the same issue. I'm using Python 3.8.2.

    I checked the _init_.py file located at /usr/lib/python3.8/urllib/ and its empty, which means you can not make a fully qualified access using only import urllib. In this case, you have to use import urllib.request.

    0 讨论(0)
  • 2020-12-17 08:47

    According to this, you have to use the following:

    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.

    0 讨论(0)
  • 2020-12-17 08:47
    import urllib.request as urllib
    

    then in the code call urlopen function

    example:

    test = urllib.urlopen("----")
    
    0 讨论(0)
  • 2020-12-17 08:49

    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
    
    0 讨论(0)
提交回复
热议问题