How do I set up module imports so that each module can access the objects of all the others?
I have a medium size Python application with modules files in various subdirectories. I have created modules that append these subdirectories to sys.path
and imports a group of modules, using import thisModule as tm
. Module objects are referred to with that qualification. I then import that module into the others with from moduleImports import *
. The code is sloppy right now and has several of these things, which are often duplicative.
First, the application is failing because some module references aren't assigned. This same code does run when unit tested.
Second, I'm worried that I'm causing a problem with recursive module imports. Importing moduleImports imports thisModule, which imports moduleImports . . . .
What is the right way to do this?
"I have a medium size Python application with modules files in various subdirectories."
Good. Make absolutely sure that each directory include a __init__.py
file, so that it's a package.
"I have created modules that append these subdirectories to sys.path
"
Bad. Use PYTHONPATH
or install the whole structure Lib/site-packages
. Don't update sys.path
dynamically. It's a bad thing. Hard to manage and maintain.
"imports a group of modules, using import thisModule as tm
."
Doesn't make sense. Perhaps you have one import thisModule as tm
for each module in your structure. This is typical, standard practice: import just the modules you need, no others.
"I then import that module into the others with from moduleImports import *
"
Bad. Don't blanket import a bunch of random stuff.
Each module should have a longish list of the specific things it needs.
import this
import that
import package.module
Explicit list. No magic. No dynamic change to sys.path
.
My current project has 100's of modules, a dozen or so packages. Each module imports just what it needs. No magic.
The way to do this is to avoid magic. In other words, if your module requires something from another module, it should import it explicitly. You shouldn't rely on things being imported automatically.
As the Zen of Python (import this
) has it, explicit is better than implicit.
You won't get recursion on imports because Python caches each module and won't reload one it already has.