Search code examples
python-3.xrelative-import

relative import in python 3 error no known parent package


I am trying to import common/util module in my mains under feature1 and feature2 directory. I have used from ..common import util but getting ImportError: attempted relative import with no know parent package. Is there a way to import common/util inside main.

Directory Stucture

common
   __init__.py
   util.py
feature1
   main.py
feature2
   main.py

Solution

  • Yes there is - run your main from the parent directory of common, feature, etc - so:

    $ ls
    common feature1 feature2
    $ python -m feature1.main
    

    Note there is no .py at the end