3

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
2
  • One approach you can take is to include the root of your project in the PYTHONPATH environment variable. Then all your imports can be "absolute" (i.e. relative to the root of your project). Commented Aug 30, 2014 at 15:24
  • That might be a solution. Commented Aug 30, 2014 at 16:45

2 Answers 2

3

Depends on where your 'main' file is.

if you have:

myproject/
  package1/
     __init__.py
     somemodule.py
  package2/
     __init__.py
     somemodule.py
     somescript.py
  main.py

somescript.py:

import package2.somemodule
import package1

main.py

import package1
import package2.somescript
import package2.somemodule

If you execute: python package2/somescript.py you get an error

Traceback (most recent call last):
  File "package2/somescript.py", line 1, in <module>
    import package2.somemodule
ImportError: No module named package2.somemodule

But if you execute python main.py, you get no problem.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this clear example. Therefore, how could I handle absolute imports in sub-modules, like somescript.py in your example?
adding import os, sys; sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))); at the begin of somescript.py maybe... But really dirty...
Not a good idea because you have to comment them out when deploying, which increases the complexity of develop-test-deploy cycle.
Edit note - main.py should be inside myproject/ instead of being outside of it. I have just done an edit to reflect this. Let me know if you think differently. Thanks.
1

One common practice is pip install -e /path_to_your_pkg/, which will create a symbolic link of your package in the Python site-packages directory (not copying it as in standard install), doing so will enable:

  • import your_pkg anywhere
  • you can keep developing/updating your package and the installed version is always the latest

One known issue is if you are following PEP 517 and replace setup.py with setup.cfg and pyproject.toml, the pip install -e does not work. You need a dump setup.py as follows to fix this compatibility issue (still an issue in Jan 2021):

import setuptools
setuptools.setup()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.