I looked through previous questions to this effect, but my situation is slightly different…
I’ve got a directory that contains several python classes that import one another here and there where necessary. I’d like to start packaging them up into smaller modules, mostly for purposes of organization. The central component of the package sets up a wsgi application and then additional modules provide functionality to it (this has worked great so far since I can test the mods individually on the command line before hooking their inputs up to the web app.)
I understand how supplying an __init__.py module works, its namespacing, and when it releases control. What I am having trouble with is that the modules in certain cases import one another.
For example, I have a JSONEncoder that is used as the cls argument for many calls to json.dumps() This class is used in virtually every file. If I move some of these modules to subdirectories within the package, how would I go “up one level” to import the json encoder? Would I need to put the parent directory on the PYTHONPATH in every file? Would it be better to give the encoder its own directory as well, with a separate __init__.py file?
Alternatively, would I just be better off putting in a function that drills down through all directories below the current working dir and adds them to the path?
Currently the main directory that contains all the code is not package in of itself… what would be the best approach to migrating like classes to subdirectories taking into account that they have circular imports?
EDIT:
For clarification:
The main working directory I’ve been developing is just a regular directory containing about ten .py files, all of which contain classes with names like their filenames. Currently the whole thing is just a git repo of .py files.
-project
search.py
jsonencoder.py
webapp.py
modela.py
modelb.py
modelc.py
Names are of course for example purposes.
Now as I understand it (which could be totally wrong) the sensible thing to do would be to package those models together, correct?
-project
search.py
jsonencoder.py
webapp.py
-models
__init__.py
modela.py
modelb.py
modelc.py
But then in turn, would the project folder need an __init__ too? How would modela, which uses jsonencoder, import it?
The answer is quite simple:
just use the fully qualified name for the sub-module you need
For example, under
you can have the line
For example, given this module structure:
with the files defined as in:
This works: