Python reload file

Sebastian picture Sebastian · Jul 14, 2015 · Viewed 16.7k times · Source

I have a script that computes some stuff. It uses inputs from a separate file 'inputs.py'.

In 'inputs.py' are only a few variables:

A = 2.3
B = 4.5
C = 3.0

In the main file I import them with

from inputs import *

If I now change something in 'inputs.py' and execute the script again it still uses the old values instead of the new ones. How can I reload the file?

reload(inputs)

does not work.

Many thanks in advance!

Answer

Anand S Kumar picture Anand S Kumar · Jul 14, 2015

If you are using Python 3.x , then to reload the names that have been imported using from module import name , you would need to do -

import importlib
import inputs #import the module here, so that it can be reloaded.
importlib.reload(inputs)
from inputs import A # or whatever name you want.

For Python 2.x , you can simply do -

import inputs   #import the module here, so that it can be reloaded.
reload(inputs)
from inputs import A # or whatever name you want.