问答详情
源自:7-4 Python发送HTTP请求

AttributeError: cannot access submodule 'request' of module 'urllib' (most likely due to a circular import)

import urllib.request

response = urllib.request.urlopen('http://127.0.0.1:9999/')
print(response)
for k, v in response.getheaders():
    print('{}: {}'.format(k, v))

我编辑了一个http.py文件,内容如上。然后在终端输入:python3 http.py

去执行它,它就会报错,如下:

Traceback (most recent call last):

  File "/Users/fangxiaoyan/Documents/pyPractise/http.py", line 3, in <module>

    import urllib.request

  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 88, in <module>

    import http.client

  File "/Users/fangxiaoyan/Documents/pyPractise/http.py", line 6, in <module>

    response = urllib.request.urlopen('http://127.0.0.1:9999')

               ^^^^^^^^^^^^^^

AttributeError: cannot access submodule 'request' of module 'urllib' (most likely due to a circular import)


但是如果我直接在终端输入python3,然后输入上面的代码去执行,又没有问题。这是为啥呢?


提问者:慕粉2044547211 2024-03-05 20:41

个回答

  • weixin_慕运维1128226
    2024-07-15 14:56:28

    pycharm环境配置问题