is this a good way to do imports ?
Terry Reedy
tjreedy at udel.edu
Wed Nov 5 18:13:43 EST 2008
Stef Mientki wrote:
> hello,
>
> I can't find any good documentation or examples of packages,
> so I'm not sure if this is good / correct way, although it seems to work.
>
> root
> / dir1
> / file1.py
> / general.py
>
> / dir2
> / file2.py
>
> / general_root.py
>
> Now I want to be able to use functions of file2 in file1,
> and vice-versa.
> In practice the directory structure is more complex and nested more deeply,
> and basically I want all modules to be reachable by all other modules.
One straightforward way is plain old absolute imports. Assume root is
in the import path (for instance, in site packages)
>
> ==== file1.py ====
> import general
from root.dir1 import general
> import file2
from root.dir2 import file2
or even
from root.dir2.file2 import func
This will break if the tree changes. So will new relative imports.
Pulling everything into root in its __init__.py breaks if there are
duplicate module names.
Anyway, above is what I am doing with my current 3-level
(pac.subpac.mod) package until I learn something really better, given
that the interdependencies in my package should remain relatively sparse.
tjr
More information about the Python-list
mailing list