I have a Bottle webserver module with the following line:
from foobar.formtools import auto_process_form_insert
And the foobar.formtools
module contains this line:
from foobar.webserver import redirect, redirect_back
Of course, both result in the following errors (respectively):
ImportError: cannot import name auto_process_form_insert
ImportError: cannot import name redirect
Is it simply a fact that in Python two modules can't import each other and all module imports must be hierarchical in nature, or am I doing something wrong? Alternatively, is there a workaround short of placing all these nice functions in new modules?
Don't do
from ... import ...
. Just doimport ...
and reference its objects using the module name.Modules can import each other cyclically, but there's a catch. In the simple case, it should work by moving the
import
statements to the bottom of the file or not using thefrom
syntax.Here's why that works:
When you import a module, Python first checks
sys.modules
. If it's in there, it just imports from there. If it's not there, it tries to import it in the normal way; basically, it finds the file and runs the stuff in it.Running a module populates the module's contents. For example, say we have this module, creatively named
example_opener
:At the start, the module is empty. Then Python executes:
After that, the module only contains
webbrowser
. Then Python executes this:Python creates
open_example
. Now the module containswebbrowser
andopen_example
.Say
webbrowser
contained this code:Say
example_opener
is imported first. This code is executed:webbrowser
has not yet been imported, so Python executes the contents ofwebbrowser
:example_opener
has been imported, but not yet fully executed. Python doesn't care. Python pulls the module out ofsys.modules
. At this point,example_opener
is still empty. It hasn't definedopen_example
yet, nor even completed importingwebbrowser
. Python can't findopen_example
inexample_opener
, so it fails.What if we imported
open_example
from the end ofwebbrowser
andwebbrowser
from the end ofexample_opener
? Python would start by executing this code:webbrowser
does not exist yet, but it doesn't matter untilopen_example
is called. Nowexample_opener
contains onlyopen_example
. It then executes:It has not been imported yet, so Python executes
webbrowser
. It starts:It defines
open
. Then it executes:example_opener
is insys.modules
, so it uses that.example_opener
containsopen_example
, so it succeeds. Python finishes importingwebbrowser
. That concludes importingwebbrowser
fromexample_opener
. That's the last thing inexample_opener
, so the import ofexample_opener
finishes, successful, as well.