I know relative imports are not suggested, such as quoted from PEP8:
Relative imports for intra-package imports are highly discouraged. Always use the absolute package path for all imports. Even now that PEP 328 is fully implemented in Python 2.5, its style of explicit relative imports is actively discouraged; absolute imports are more portable and usually more readable.
What if I am developing a package (with several modules)? While in development, absolute import won't work w/o installing/deploying the package. Does that mean I have to periodically install/deploy the current WIP modules just for testing?
Here is an example provided by Cld. Given a Python project/package:
myproject/
package1/
__init__.py
somemodule.py
package2/
__init__.py
somemodule.py
somescript.py
main.py
In main.py, absolute-import works quite well:
import package1
import package2.somescript
import package2.somemodule
However, for modules, like somescript.py in package2, the following absolute-imports:
import package2.somemodule
import package1
It would raise ImportError:
Traceback (most recent call last):
File "package2/somescript.py", line 1, in <module>
import package2.somemodule
ImportError: No module named package2.somemodule
PYTHONPATHenvironment variable. Then all your imports can be "absolute" (i.e. relative to the root of your project).