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

来源:7-4 Python发送HTTP请求

慕粉2044547211

2024-03-05 20:41

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,然后输入上面的代码去执行,又没有问题。这是为啥呢?


写回答 关注

1回答

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

    pycharm环境配置问题

Python3 进阶教程(新版)

学习函数式、模块和面向对象编程,掌握Python高级程序设计

41910 学习 · 236 问题

查看课程

相似问题