I have a question about imports. The question might seem a bit contrived, but its purpose is to explore the limitations of using absolute imports for all imports in a package. PEP8 highly discourages relative imports (edit: and the Google Python Style Guide says never to use them).
Say you are given two large-ish packages that both have the same name and that both use absolute imports in accordance with PEP8:
/pkg1
mod1.py (contains an absolute import: 'import pkg1.mod2')
mod2.py
...
/pkg1
mod1.py (contains an absolute import: 'import pkg1.mod3')
mod3.py
...
Also say you are working on a Python project in which you'd like to use both packages. This could come up, say, if you want to use two versions of the same package in a project.
Is there a way to incorporate both packages into your project hierarchy so that you can freely use modules from both packages throughout your project?
For the solution it's acceptable to do things like use import aliases and modify sys.path temporarily. But it's not okay to change the contents of either package directory.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…