1

I followed Python pattern for sharing configuration throughout application and failed once I tried using it from a different package (folder), can anyone offer a fix?

tree:

|-- my_app | |-- config.py | |-- main.py | |-- my_package | | |-- also.py 

command line: python -m my_app.main

both main.py and also.py need to use the global configuration from config.py. code in both of them:

import config print("config", config) 

displays 2 different things!!! main.py says:

('config', <module 'my_app.config' from 'my_app/config.pyc'>) 

and also.py says:

('config', <module 'config' from '/tmp/project/my_app/config.pyc'>) 

and it breaks the shared configuration of course :(

I tried following traps but couldn't find anything obvious...

6
  • 1
    It's very obviously the "double import trap" documented in your link. You didn't specify which Python version you were using, but changing both imports to from myapp import config might solve the problem. Commented Dec 13, 2018 at 10:30
  • thanks @brunodesthuilliers - that fixed the problem indeed (wasn't obvious to me :) . Commented Dec 13, 2018 at 11:12
  • I discovered this trap the hard way with early django versions so I now know how to spot it ;-) The telltale sign is the difference in module path. Commented Dec 13, 2018 at 11:33
  • @brunodesthuilliers - help! now my py.test wont run! py.test my_app/tests throws No module named my_app Commented Dec 13, 2018 at 13:20
  • 1
    @brunodesthuilliers - agree, found the solution - I should use python -m pytest my_app/tests instead Commented Dec 13, 2018 at 14:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.