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...

ihadanny
  • 4,377
  • 7
  • 45
  • 76
  • 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. – bruno desthuilliers Dec 13 '18 at 10:30
  • thanks @brunodesthuilliers - that fixed the problem indeed (wasn't obvious to me :) . – ihadanny Dec 13 '18 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. – bruno desthuilliers Dec 13 '18 at 11:33
  • @brunodesthuilliers - help! now my py.test wont run! `py.test my_app/tests` throws `No module named my_app` – ihadanny Dec 13 '18 at 13:20
  • This is another distinct problem so please open a new question with the relevant details (including the full traceback) - but first search for similar question, since it's certainly already been asked here. – bruno desthuilliers Dec 13 '18 at 13:27
  • 1
    @brunodesthuilliers - agree, found the solution - I should use `python -m pytest my_app/tests` instead – ihadanny Dec 13 '18 at 14:05

0 Answers0