No module named ‘urllib.request’; ‘urllib’ is not a package

How is 618 sales champion made?Uncover the secret of e-commerce’s “invigorating” hundreds of millions of sales data>>>

I want to learn how to set up proxy server for crawler urllib, so I picked up urllib that I didn’t learn before and typed a simple code as follows

import urllib.request

url = "http://www.baidu.com"
data = urllib.request.urlopen(url).read()
data = data.decode('UTF-8')
print(data)

However, errors are always reported after execution

Traceback (most recent call last):
  File "urllib.py", line 1, in <module>
    import urllib.request
  File "F:\python\urllib.py", line 1, in <module>
    import urllib.request
ImportError: No module named 'urllib.request'; 'urllib' is not a package

I’ve tried to use fromurl lib import request, PIP install urllib

Or the same problem. After saving py file, you will be prompted to import rror:Nomodulenamedrequest 。

But it works in Python shell

Many search, found that is too silly x error

It turned out to be my file naming problem

The priority of system search module is: program main directory, then the path defined by system environment variables, and then the standard library directory. If we find it in this order, we will not look down. Because the file name is exactly urllib, I intended to import urllib under the directory of the standard library. As a result, I imported my current file

Reflection:

I found it in stack overflow at the beginning, but when I saw that the background of the problem was version 2.7, I didn’t continue to look down. I firmly believed that the application method of urllib was wrong, and that there was no reference value if it wasn’t version 3.6

There are some differences between python2.7 and 3. X code, but the reason for the error may be the same

Similar Posts: