Search code examples
pythonpython-3.xpackagingsetup.py

import sub-packages or module from directory of setup.py


I started making a package (an executable in fact) and made a small program that contains several files in a directory structure:

my_app_proj
  |-setup.py
  |-my_app
     |-my_app.py
     |-__init__.py
     |-lib
        |-libA.py
        |-libB.py

In the file my_prog.py, I have something like:

from lib import libA
from lib import libB

But when I want to create my package with setup.py,the import directives are no longer operational because they are not in the PYTHONPATH. Obviously since I am in the parent directory of the my_prog directory.

One solution is to add in the file __init__.py the path of my_prog in the python path, but I find it crappy.

Is there a more elegant solution?


Solution

  • You can use relative import :

    from .lib import libA