import error in python even after having init file

2019-09-10 18:07发布

问题:

I am trying to import a file in python

my directory structure is

maindir
    __init__.py
    constants.py

    subdirectory
          __init__.py
          code.py

I am trying to import constants in the code file

import sys
sys.path.append('/home/ching/maindir')
from maindir import constants

even after that I am getting this error

ImportError: No module named maindir

Any one got any idea whats wrong?

回答1:

Try this:

import sys
sys.path.append('/home/ching')
from maindir import constants