Post 在 python 解释器中 运行 时有效,但在另存为 .py 时出现 AttributeError
Post works when run in python interpreter but gets AttributeError when saved as .py
我创建了一个简单的邮件应用程序作为通知。当使用 python 解释器进行测试时,它可以完美运行。但是如果我 运行 它作为一个独立的应用程序 (.py) 它说 AttributeError: module 'requests' has no attribute 'post'.
import requests
def send_simple_message():
return requests.post(
"https://...",
auth=("api", "key"),
data={"from": "from",
"to": "to",
"subject": "Hello World",
"text": "Text"})
send_simple_message()
它还在目录
中创建了一个不寻常的文件夹
___pycache___
我收到的错误是:
AttributeError: module 'requests' has no attribute post
奇怪的是,在解释器上 运行 时,代码能够毫无错误地发送 post。
当您的文件名与模块名相同时会出现此错误,
例如将您的文件命名为 requests.py
将导致此错误
为了避免您必须将其名称更改为 requests.py
以外的任何名称
我创建了一个简单的邮件应用程序作为通知。当使用 python 解释器进行测试时,它可以完美运行。但是如果我 运行 它作为一个独立的应用程序 (.py) 它说 AttributeError: module 'requests' has no attribute 'post'.
import requests
def send_simple_message():
return requests.post(
"https://...",
auth=("api", "key"),
data={"from": "from",
"to": "to",
"subject": "Hello World",
"text": "Text"})
send_simple_message()
它还在目录
中创建了一个不寻常的文件夹___pycache___
我收到的错误是:
AttributeError: module 'requests' has no attribute post
奇怪的是,在解释器上 运行 时,代码能够毫无错误地发送 post。
当您的文件名与模块名相同时会出现此错误,
例如将您的文件命名为 requests.py
将导致此错误
为了避免您必须将其名称更改为 requests.py