Python - 模块“requests”没有属性“get”

问题描述 投票:0回答:1

我尝试在 MacOS 上的 Python 3 中使用

requests
并得到

AttributeError: module 'requests' has no attribute 'get'

Python 是随brew 安装的,requests 也是如此 - 我做到了

brew install python-requests

我的脚本以

#!/usr/bin/env python3
开头,它从brew 中引入 Python 3.12。

我的脚本不是名为requests.py,据我所知,任何地方都没有requests.py。

这看起来令人费解:

% /usr/bin/env python3
Python 3.12.2 (main, Feb  6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.get('https://google.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'requests' has no attribute 'get'
>>> print("requests file:", requests.__file__)
requests file: None
>>> 
python macos python-requests homebrew
1个回答
-1
投票

我无法在我的环境中重现它,请确保您没有将文件命名为 requests.py

dysondi@MYPC:/tmp$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.get('https://google.com')
<Response [200]>
>>> 
© www.soinside.com 2019 - 2024. All rights reserved.