Search code examples
pythonpostpython-requestsmailgunattributeerror

Post works when run in python interpreter but gets AttributeError when saved as .py


I created a simple mailing app as a notification. When tested using python interpreter it works flawlessly. But if I run it as a stand alone application (.py) it says 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()

It also creates a unusual folder in the directory

___pycache___

The error I receive is:

AttributeError: module 'requests' has no attribute post

The weird thing is the code is able to send the post without any errors when run on interpreter.


Solution

  • This error occurs when your file name is same as the module name,

    for example naming your file to requests.py will cause this error

    so to avoid that you have to change its name to anything other than requests.py