Importing Installed Package from Script With the Same Name Raises "Attributeerror: Module Has No Attribute" or "Importerror: Cannot Import Name"

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

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]>.

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

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]>.

ModuleNotFoundError: No module named 'collections.abc'; 'collections' is not a package

You've got a file called collections.py in your /app folder. So it's trying to import it from there rather than the actual python-collections module. Rename yours to not clash with the names of python built-in modules, like my_collections.py, and retry.

python import does not work in scripts

You have a local file named org.py that shadows an optional built-in package (only used in Jython).

From your traceback:

  File "C:\Python3\lib\site-packages\numpy\core\numeric.py", line 37, in 
import pickle
File "C:\Python3\lib\pickle.py", line 93, in
from org.python.core import PyStringMap
File "C:\Users\Jakub\desktop\Nowy\workspace\python\org.py", line 2, in <module>
from tensorflow.examples.tutorials.mnist import input_data

C:\Users\Jakub\desktop\Nowy\workspace\python\org.py is not the Jython package the pickle module looks for.

You only see this exception when you run your script because the directory your script is located in (C:\Users\Jakub\desktop\Nowy\workspace\python here) is added to sys.path automatically.

The attribute error is caused by the circular import; numpy imports pickle which imports your org script again, which imports numpy, but that module has not yet completed all its imports, so names are missing at this time.

ImportError: cannot import name 'Celery'

Do not put same name of your package and system package as it creates confusion for python when you hit import statement. In your case you name your package celery which is also a name of original celery package. In short simply rename your celery folder to something else.



Related Topics



Leave a reply



Submit