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"

4
  • stackoverflow.com/a/33068321/6253693 seems like an option for reloading in a function Commented Apr 30, 2018 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. Commented Apr 30, 2018 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. Commented Apr 30, 2018 at 17:42
  • it's not a big script but i'll keep that in mind, thanks Commented May 2, 2018 at 8:07

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.