how to import nested module from nested module
Question: Simple question, but could not find the answer. I've following structure:

    
    
    ./lib1:
    main.py
    
    ./lib2:
    __init__.py  utils.py
    

From the root diretory, I'm running:

    
    
    python lib1/main.py
    

and in main.py I want to import lib2/utils.py.

adding import lib2/utils.py fails. One solution I found is to add:

    
    
    ~/tmp/root$ cat lib1/main.py
    
    import sys,os
    sys.path.append(os.getcwd())
    import lib2.utils
    
    lib2.utils.foo()
    

which is good, but I wander if there is other solution. Thanks.


Answer: Are `lib1` and `lib2` separate modules? If yes, the comment by @BrenBarn
applies: You need to add the top directory (containing `lib1` and `lib2` to
the Python path (e.g using `PYTHONPATH` environment variable or appending to
`sys.path`).

If both `lib1` and `lib2` are part of one module (i.e. there is a
`__init__.py` file in the top directory) you can use relative imports
(<https://docs.python.org/2.5/whatsnew/pep-328.html>).

