Absolute Python import in module fails

I have a project that looks like this:

my_project/
          __init__.py -- empty
          run.py
          datacheck/
                  __init__.py -- empty
                  datacheck.py -- containing class DataCheck(object)
                  config.py -- containing BusinessConfig(object)
                  business.py -- containing class BusinessCheck(DataCheck)

My is PYTHONPATHconfigured to have / my _project in it.

In run.py, I have the following code:

from datacheck.business import BusinessCheck
business = BusinessCheck()
business.check_data()

In business.py, I have the following imports that are not executed:

from datacheck.config import BusinessConfig
from datacheck.datacheck import DataCheck

Relative imports, such as from .config import BusinessConfig, work, however I read in numerous threads that absolute imports are preferable.

To make a simple test, I also created the following:

myproject/
          __init__.py -- empty
          run_test.py
          test/
              __init__.py -- empty
              test1.py -- containing class Test1(object)
              test2.py -- containing class Test2(Test1)

run_test.py imports and runs the class Test2, this did not work.

It stunned me a bit, I don’t understand why my absolute import in datacheck doesn’t work - can anyone explain?

+5
2

, __main__, . , .

, datacheck datacheck ( ). Python . datacheck. config, .

, datacheck __init__.py .

+6

, , , , :

from __future__ import absolute_import

Python 2.6 2.7.

+2

All Articles