Frage

Ich habe zwei Python -Module:

A.Py

import b

def hello():
  print "hello"

print "a.py"
print hello()
print b.hi()

B.Py

import a

def hi():
  print "hi"

Wenn ich renne a.py, Ich bekomme:

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

Was bedeutet der Fehler? Wie repariere ich es?

War es hilfreich?

Lösung

Sie haben gegenseitige Importe auf der obersten Ebene, was fast immer eine schlechte Idee ist.

Wenn Sie wirklich gegenseitige Importe in Python haben müssen, können Sie sie innerhalb einer Funktion importieren:

# In b.py:
def cause_a_to_do_something():
    import a
    a.do_something()

Jetzt kann a.py sicher tun import b ohne Probleme zu verursachen.

(Auf den ersten Blick könnte es erscheinen, dass das erscheinen könnte cause_a_to_do_something() wäre sehr ineffizient, weil es eine tut import Jedes Mal, wenn Sie es nennen, aber tatsächlich wird die Importarbeit erst beim ersten Mal erledigt. Die zweite und nachfolgende Zeit, in der Sie ein Modul importieren, ist es ein schneller Betrieb.)

Andere Tipps

I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.

The problem is the circular dependency between the modules. a imports b and b imports a. But one of them needs to be loaded first - in this case python ends up initializing module a before b and b.hi() doesn't exist yet when you try to access it in a.

I got this error by referencing an enum which was imported in a wrong way, e.g.:

from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member

Correct import:

from package.MyEnumClass import MyEnumClass

Hope that helps someone

I experienced this error because the module was not actually imported. The code looked like this:

import a.b, a.c

# ...

something(a.b)
something(a.c)
something(a.d)  # My addition, which failed.

The last line resulted in an AttributeError. The cause was that I had failed to notice that the submodules of a (a.b and a.c) were explicitly imported, and assumed that the import statement actually imported a.

All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.

I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py files, but left the untracked .pyc files. Since the .py files and .pyc files were out of sync, the import command in a .py file could not find the corresponding module in the .pyc files.

The solution was simply to delete the .pyc files, and let them be automatically regenerated.

I faced the same issue. fixed by using reload.

import the_module_name
from importlib import reload
reload(the_module_name)

Not sure how but the below change sorted my issue:

i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .

Hope so it helps

Circular imports cause problems, but Python has ways to mitigate it built-in.

The problem is when you run python a.py, it runs a.py but not mark it imported as a module. So in turn a.py -> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi(), it can't find anything.

Note that the b.hi() that got executed is during a.py -> module b -> module a, not in a.py directly.

In your specific example, you can just run python -c 'import a' at top-level, so the first execution of a.py is registered as importing a module.

@MD5 Same for me, Try changing the module name or import the function directly

on ubuntu 18.04 ( virtualenv, python.3.6.x), the following reload snippet solved the problem for me:

main.py

import my_module  # my_modyle.py
from importlib import reload # reload 
reload(my_module)

print(my_module)
print(my_modeule.hello())

where:

|--main.py    
|--my_module.py

for more documentation check : here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top