Import From Parent Folder

Search Path

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

Directory Tree

$ tree importtests
importtests
├── common
│   ├── __init__.py
│   ├── aaa.py
└── function
    ├── __init__.py
    └── bbb.py
  • aaa.py

    $ cat common/aaa.py
    def say_hi():
        print 'Python Cook Book'
    
  • bbb.py

    $ cat function/bbb.py
    from common.aaa import say_hi
    
    say_hi()
    
  • Exec

    $ python bbb.py
    Traceback (most recent call last):
      File "bbb.py", line 14, in <module>
        from common.aaa import say_hi
    ImportError: No module named common.aaa
    

Solution No.1

$ cat function/bbb.py
import sys
sys.path.append('..')

from common.aaa import say_hi

say_hi()
  • Exce in function dir

    $ python bbb.py
    Python Cook Book
    
  • Exec in importtests dir

    $ python function/bbb.py
    Traceback (most recent call last):
      File "function/bbb.py", line 12, in <module>
        from common.aaa import say_hi
    ImportError: No module named common.aaa
    

Solution No.2

Use 3rd Libray path.py

$ cat function/bbb.py
import path
import sys

folder = path.path(__file__).abspath()
sys.path.append(folder.parent.parent)

from common.aaa import say_hi

say_hi()
  • Installation

    pip install path.py
    
  • Exce in function dir

    $ python bbb.py
    Python Cook Book
    
  • Exec in importtests dir

    $ python function/bbb.py
    Python Cook Book
    

Solution No.3

Use Standard Libray os.path

$ cat function/bbb.py
import os
import sys

root_folder = os.path.abspath(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(root_folder)

from common.aaa import say_hi

say_hi()
  • Exce in function dir

    $ python bbb.py
    Python Cook Book
    
  • Exec in importtests dir

    $ python function/bbb.py
    Python Cook Book
    

Solution No.4

Use explicit relative import

$ cat function/bbb.py
# from .. common.aaa import say_hi  # Or As Below
from ..common.aaa import say_hi

say_hi()
  • Exec in importtests' parent dir

    $ python -m importtests.function.bbb
    Python Cook Book
    
    $ python -m importtests.function.bbb.py
    Python Cook Book
    /Users/xxx/venv/bin/python: No module named importtests.function.bbb.py
    

APPEND or INSERT

  • sys.path.append(folder.parent.parent)
  • sys.path.insert(0, folder.parent.parent)
    • Recommendation

References

[1] [email protected], 6. Modules — 6.1.2. The Module Search Path

[2] [email protected], path.py — "Path" object conveniently wrapping assorted file/path-related functionality

results matching ""

    No results matching ""