Import variables into functions from external file in Python
Question: I am trying to write a script that is rather long, uses several different data
sources and can go wrong at many different stages. Having to restart the whole
process from the beginning each time some error in the input data is
discovered is not too much fun so I though I would save variables (actually
paths to created data files) to a backup file then read in this file and pick
up where I left off. Unfortunately

    
    
    from previousrun import *
    

only imports the variables locally and I can't use import in each function as
Python tells me its not allowed at module level. Is there any way of importing
an unknown number of variables from another file and have them globally
available?


Answer: Use this in your function:

    
    
    `locals().update(importlib.import_module("importlib").__dict__)`
    

and `import importlib`.

