Import urllib.request, ImportError: No module name

2020-02-08 12:41发布

问题:

I am trying to import urllib.request for python 2.7.10 on PyCharm 4.5.4 on Window 10 but getting the error "ImportError: No module named request".

回答1:

The urllib.request modules have been deprecated .. just use

import urllib

And for your function if you were earlier writing say

urllib.request.urlretrieve

Now you just write

urllib.urlretrieve


回答2:

I have also faced the same error and Googled to solve it. urlib.request is for Python 3.0.

You may use the code below:

import urllib
urllib.urlopen(url)


回答3:

You'll get this error if you try running a python 3 file with python 2.



回答4:

Try to use this in Python3

try:
    x = urllib.request.urlopen('https://www.google.com/search?q=test')
    print(x.read())

except Exception as e:
    print(str(e))


回答5:

Use > Path\easy_install.exe requests if you have a windows machine, where easy_install can be found in your Python*\Scripts folder, if it was installed. (Note Path\easy_install.exe is an example, mine is C:\Python32\Scripts\easy_install.exe)

If you don't have easy install and are running on a windows machine, you can get it here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#distribute

If you manually want to add a library to a windows machine, you can download the compressed library, uncompress it, and then place it into the Lib folder of your python path.

OR

You need to install pip first and then install django-request using pip

pip install django-request

also install,

python setup.py install

then import

from urllib.request import urlopen

Helpful Tips:to chech this