When you name your script with the same name as an imported library, such as for instance requests.py, various import issues can arise. These issues can manifest as AttributeErrors, ImportErrors, or NameErrors depending on the import approach used.
This occurs because the script's name shadows the installed library in sys.path, giving precedence to the local script over the intended import.
import requests res = requests.get('http://www.google.ca') print(res)
from requests import get res = get('http://www.google.ca') print(res)
from requests.auth import AuthBase
from requests import * res = get('http://www.google.ca') print(res)
To resolve this issue, rename your script to a different name that does not conflict with any imported modules. Additionally, delete the generated requests.pyc file (if present) to prevent interference from the cached bytecode.
When encountering these errors, examine the traceback carefully to identify the module name collision between the script name and the imported module.
The above is the detailed content of What Happens When Your Python Script's Name Conflicts with an Imported Library?. For more information, please follow other related articles on the PHP Chinese website!