This template is fine to build packaged applications however it could be more useful if it could be able to generate code with absolute path.
Relative import in code makes it not so easy to include generated application code into an existing Django project (when the application code is just copied and pasted into a project tree). It can work for some project but there is many case when it won't and for this, using absolute import path would makes it easier.
It would need two things:
- Rewrite all package relative imports to absolute starting with application module name;
- Optional prefix to add before the application module name for projects which hold their application inside another module instead of being directly defined in sys.path;
So for a package with application name dummy
, these relative paths from module dummy/sample.py
:
import .foo
from .bar import ping
(so it imports dummy/foo.py
and dummy/bar.py
)
Would be turned to:
import dummy.foo
from dummy.bar import ping
And if optional prefix project
is given it would be turned to:
import project.dummy.foo
from project.dummy.bar import ping
This should be applied to application code since test already used absolute path, however it is the package application absolute path so the prefix would be missing, but it would require a different rewriter to apply, i consider this is not important for now.
Concerns
Relative path may be not so simple to resolve in some cases, it need to be studied.
Alternative
I though some Python would already exists for this kind of import rewriting but it seems not. I searched on isort, something has been thinked in an issue but it is still opened and not implemented.
I've also found a tool like https://github.com/MarcoGorelli/absolufy-imports but it is not maintained anymore, maybe we could at least use some part of its code.