1

i have a configuration file in python that can be changed while the main script is running, so i need to reload it.

i tried the answer in this post: python refresh/reload

import config
from config import *
...
reload(config)
from config import *

it worked until i entered the reload part to a function, so if i do this:

import config
from config import *

def main():
    reload(config)
    from config import *

i get the warning: 'import *' only allowed in module level, the script is running but the reload isn't working,

i also tried "import config" instead of "from config import *" but i got an exception "UnboundLocalError: local variable 'config' referenced before assignment"

Ortal Turgeman
  • 143
  • 4
  • 14
  • https://stackoverflow.com/a/33068321/6253693 seems like an option for reloading in a function – Ari K Apr 30 '18 at 17:37
  • Using a module like that as a config file is _not_ a good strategy. Reloading modules is just a convenience while you're developing. You shouldn't do it in production code, because there's a good chance that not everything will get changed by the reloading process. Use a proper config file. – PM 2Ring Apr 30 '18 at 17:39
  • 1
    Also, "star" imports are best avoided in normal scripts: they dump a bunch of names into your namespace making it impossible to distinguish the local names from the imported ones, and possibly leading to name collisions, especially if you do star imports from more than one module. – PM 2Ring Apr 30 '18 at 17:42
  • it's not a big script but i'll keep that in mind, thanks – Ortal Turgeman May 02 '18 at 08:07

1 Answers1

1

I suggest you to store your configuration in a file rather than a module. Please take a look at the ConfigParser module from Python. From what I've heard, it can handle ini files too.

Arnev Sai
  • 171
  • 3