Search code examples
pythonmodulepackagepython-modulepython-import

How can I properly use relative or absolute imports in Python modules?


Usage of relative imports in Python has one drawback; you will not be able to run the modules as standalones anymore, because you will get an exception:

ValueError: Attempted relative import in non-package

Code

# /test.py: just a sample file importing foo module
import foo
...

# /foo/foo.py:
from . import bar
...
if __name__ == "__main__":
   pass

# /foo/bar.py: a submodule of foo, used by foo.py
from . import foo
...
if __name__ == "__main__":
   pass

How should I modify the sample code in order to be able to execute all of the following? test.py, foo.py and bar.py.

I'm looking for a solution that works with Python 2.6+ (including 3.x).


Solution

  • First, I assume you realize what you've written would lead to a circular import issue, because foo imports bar and viceversa; try adding

    from foo import bar
    

    to test.py, and you'll see it fails. The example must be changed in order to work.

    So, what you're asking is really to fallback to absolute import when relative import fails; in fact, if you're executing foo.py or bar.py as the main module, the other modules will just lie at the root level, and if they share the name with another module on the system which one will be picked depends on the order in sys.path. Since the current dir is usually the first, local modules will be picked if available - i.e., if you've got an 'os.py' file in the current working dir, it'll be picked instead of the builtin one.

    A possibile suggestion is:

    foo.py

    try:
        from . import bar
    except ValueError:
        import bar
    
    if __name__ == "__main__":
        pass
    

    bar.py:

    if __name__ == "__main__":
        pass
    

    By the way calling scripts from the proper position is usually way better.

    python -m foo.bar
    

    Is probably the best way to go. This runs the module as a script.