0

[Structure]

 Root
  |_ app
      |_util
          |_ log.py
      |_config.py
  |_ run.py

[run.py]

#import sys
#import os
#sys.path.insert(0, os.path.abspath('.'))

from app.config import Config

class App(object):
    def __init__(self):
        pass

    def run(self):
        Config.set_code(200)
        Config.print_code()

if __name__ == '__main__':
    app = App()
    app.run()

[config.py]

from app.util.log import Log

class Config(object):
    code = None
    def __init__(self):
        pass

    @classmethod
    def set_code(cls, code):
        cls.code = code    

    @classmethod
    def get_code(cls):
        return cls.code

    @classmethod
    def print_code(cls):
        Log.msg()

[log.py]

from app.config import Config

class Log(object):
    def __init__(self):
        pass

    @classmethod
    def msg(cls):
        code = Config.get_code()
        print('Code is: %r' % code)    

[Current Output]

File "C:\works\workout\tmp\app\util\log.py", line 1, in <module>
    from app.config import Config

ImportError: cannot import name 'Config' from 'app.config' (C:\works\workout\tmp\app\config.py)

[Expected Output]

Code is: 200

Another one doubt, is there any way we can fix root import as 'app', then whatever inside this folder/sub folder files (any kind of depth) access by,

[app/sub1/a.py]

 from app.util.xx import XX
 **from app.a import A**
 from app.aaa.bb import BB

 A.some_fn()

[app/a.py]

 from app.util.xx import XX
 **from app.sub1.a import A**
 from app.aaa.bb import BB 

 A.some_fn()

[app/sub1/sub2/a.py]

 **from app.sub1.a import A**
 **from app.sub1.sub2.b import B**
 from app.aaa.bb import BB 

 A.some_fn()

If any suggestions or ideas, then please! Thanks in advance,

Jai K
  • 375
  • 1
  • 4
  • 12
  • Looks like you have a circular import: config import from log and log imports from config. – Carlos Mermingas Nov 06 '18 at 13:50
  • @CarlosMermingas Yes Carlos, But I have the situation to inter communicate among those files as given above... If any other alternative work around, then pl comment. Thanks, – Jai K Nov 06 '18 at 14:33
  • 1
    Avoid circular imports. I would remove the `print_code` method from `Config`. You can call `log.msg(config.get_code())` from other modules when you need to print it. – Carlos Mermingas Nov 07 '18 at 13:07
  • @CarlosMermingas Thanks, But I have one doubt, for simple project this is Ok, but for Operating System kind of complex project, this situation is definitely will come.. at that time how they will manage..? (In our case, without removing the `print_code`) – Jai K Nov 08 '18 at 06:15
  • 1
    I would avoid circular imports, regardless of project size. If you must have it, then follow @flyingdutchman’s recommendations. – Carlos Mermingas Nov 08 '18 at 22:48

1 Answers1

0

The cyclic dependency between log.py and config.py is the problem. A lot of times cyclic dependencies can be avoided. So I would suggest to try that first.

But if you want to do it with the cyclic dependency, you can do it as described here.

For your solution it would look like this. Adapt in log.py:

from app.config import Config to import app.config

and

code = Config.get_code() to code = app.config.Config.get_code()

flyingdutchman
  • 1,197
  • 11
  • 17
  • Thanks, Working in this way, but I wouldn't like to use the full namespace path like `app.config.Config.getCode()`, I think, it's not a better coding standard. – Jai K Nov 08 '18 at 06:24
  • You're welcome. You can then e.g. use `import app.config as conf`. If this solved please accept, then it is marked as answered. – flyingdutchman Nov 08 '18 at 09:04