Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name"

PythonExceptionPython ModuleShadowing

Python Problem Overview


I have a script named requests.py that imports the requests package. The script either can't access attributes from the package, or can't import them. Why isn't this working and how do I fix it?

The following code raises an AttributeError.

import requests

res = requests.get('http://www.google.ca')
print(res)
Traceback (most recent call last):
  File "/Users/me/dev/rough/requests.py", line 1, in <module>
    import requests
  File "/Users/me/dev/rough/requests.py", line 3, in <module>
    requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'

The following code raises an ImportError.

from requests import get

res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
  File "requests.py", line 1, in <module>
    from requests import get
  File "/Users/me/dev/rough/requests.py", line 1, in <module>
    from requests import get
ImportError: cannot import name 'get'

Or code that imports from a module inside the requests package:

from requests.auth import AuthBase
Traceback (most recent call last):
  File "requests.py", line 1, in <module>
    from requests.auth import AuthBase
  File "/Users/me/dev/rough/requests.py", line 1, in <module>
    from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package

Python Solutions


Solution 1 - Python

This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name.

An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:

Notice the name you used in your script:

File "/Users/me/dev/rough/requests.py", line 1, in <module>

The module you are trying to import: requests

Rename your module to something else to avoid the name collision.

Python may generate a requests.pyc file next to your requests.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.

In the example, renaming the file to my_requests.py, removing requests.pyc, and running again successfully prints <Response [200]>.

Solution 2 - Python

For the writer of the original question, and for those people searching on the “AttributeError: module has no attribute” string, then the common explanation as per the accepted answer, is that a user-created script has a name-clash with a library filename. Note, however, that the trouble might not be in the name of the script that generates the error (as it was in the above case), nor in any of the names of the library modules explicitly imported by that script. It might take a little detective work to figure out which file is causing the problem.

As an example to illustrate the problem, imagine that you are creating a script that uses the "decimal" library for accurate floating-point calculations with decimal numbers, and you name your script "mydecimal.py" that contains the line "import decimal". There's no problem with any of that but you find that it raises this error:

AttributeError: 'module' object has no attribute 'Number'

This would happen if you had previously written a script called "numbers.py" because the "decimal" library calls on the standard library "numbers" but finds your old script instead. Even if you had deleted that, it might not end the problem because python might have converted that into bytecode and stored it in a cache as "numbers.pyc", so you'd have to hunt that down as well.

Solution 3 - Python

Python is looking for the requests object within your requests.py module.

Either RENAME that file to something else or use

from __future__ import absolute_import 

at the top of your requests.py module.

Solution 4 - Python

By import requests you import the file requests.py, not the module. Simply rename your file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionidjawView Question on Stackoverflow
Solution 1 - PythonidjawView Answer on Stackoverflow
Solution 2 - PythonDave RoveView Answer on Stackoverflow
Solution 3 - Pythonhassanzadeh.sdView Answer on Stackoverflow
Solution 4 - PythonKrzysiekView Answer on Stackoverflow