How can I override a constant in an imported Python module?

后端 未结 4 801
再見小時候
再見小時候 2020-12-17 10:33

In my application I am using module within the package example called examplemod.

My app:

from example imp         


        
4条回答
  •  没有蜡笔的小新
    2020-12-17 11:08

    Thank you all for your answers. They pointed me in the right direction though none of them worked as written. I ended up doing the following:

    import example.config
    example.config.CONSTANT = "Better value"
    
    from example import examplemod
    examplemod.do_stuff()
    # desired result!
    

    (Also, I'm submitting a patch to the module maintainer to make CONSTANT a configurable option so I don't have to do this but need to install the stock module in the mean time.)

提交回复
热议问题