How can I make python script(X) reload dynamically changing variables in another module(Y) and then re-import updated module(Y) in same script(X)?
<p>I'm new to Python, I'm stuck with a code. I have tried my best to show my problem with below sample code. I'm playing with 4 files.</p>

<p>This is the runme file. That I'm running.</p>

<p>command > python runme.py</p>

<pre><code>import os
with open("schooldata.txt", "r") as filestream:    #opening schooldata.txt file
    for line in filestream:
        currentline = line.split(",")
        a = currentline[0]
        b = currentline[1]
        c = currentline[2]

#creating a school_info.py file with value of a,b,c that are further imported by mainfile.py

        f = open('school_info.py','w')
        f.write("a= \"" + currentline[1] + "\"\n")
        f.write("b= \"" + currentline[2] + "\"\n")
        f.write("c= \"" + currentline[3] + "\"\n")
        f.close()

#importing mainfile.py and calling its functions.

        from mainfile import give_to_student
        give_to_student("Rickon")


        from mainfile import give_to_teacher
        give_to_student("Carolina")
</code></pre>

<p>Second file is schooldata.txt from where I want to read the value of a,b,c. This is our main school data file from which we take authorization data. I'm reading line by line from this file and creating a,b,c by splitting it with (,).</p>

<pre><code>12313,mshd1732,2718230efd,
fhwfw,382842324,238423049234230,
fesj32,282342rnfewk,43094309432,
fskkfns,48r209420fjwkfwk,2932042fsdfs,
38234290,fsfjskfjsdf,2942094929423,
</code></pre>

<p>Third file is school_info.py which I'm creating with this data everytime. This file is created everytime when a line is read from schooldata.txt file. So fresh file everytime with fresh and unique data of a,b,c.</p>

<pre><code>a = "asb12"
b = "121002"
c = "mya122344"
</code></pre>

<p>Now here comes the mainfile.py which is having functions like give_to_student and give_to_teacher. This file is importing data from school_info.py, so as to create authorization code using values of a,b,c. </p>

<p>and function definition of give_to_student and give_to_teacher which uses these function definitions.</p>

<pre><code>import os
import schoollib   #(internal school lib)

#importing School_info.py file so as to get value of a,b,c
from school_info import *

#It creates authorisation code internally
lock = auth(a,b,c,d)

#This authorisation code is used to call internal function

def give_to_student():
   lock.give(student)

def give_to_teacher():
   lock.give(teacher)
</code></pre>

<p>So now let me share the exact problem that I'm facing as of now, I'm unable to get authorization code loaded for mainfile.py everytime it is imported in runme.py file. When I'm calling runme.py file it is giving same authorization code to all users every time. </p>

<p>It is not able to use authorization code that is create after reading second line of schooldata.txt</p>

<p>With mainfile.py file If I'm trying to reload module using. import importlib and then importlib.reload(mainfile.py) in runme.py. </p>

<pre><code>#Added it in runme.py file

import importlib
importlib.reload(mainfile)
</code></pre>

<p>It is still giving authorization for first line of data(schooldata.txt).</p>

<p>Similar thing I tried in mainfile.py. 
I tried to import importlib and then importlib.reload(school_info).</p>

<pre><code>#added it in mainfile.py

import importlib
importlib.reload(school_info)
</code></pre>

<p>importlib.reload(school_info)
NameError: name 'school_info' is not defined</p>

<p>But it giving error, that school_info module doesn't exist.
Please throw some light on it, and how can I make it work.</p>

<p>P.S. I'm using python 3.5. Thanks</p>

<p>Why don't you try to combine the school_info.py and mainfile.py.
If you can run a combined loop.</p>

<pre><code>import os
import schoollib   #(internal school lib)
with open("schooldata.txt", "r") as filestream:    #opening schooldata.txt file
    for line in filestream:
      currentline = line.split(",")
      a = currentline[0]
      b = currentline[1]
      c = currentline[2]


#It creates authorisation code internally
      lock = auth(a,b,c,d)

#This authorisation code is used to call internal function

      def give_to_student():
         lock.give(student)

      def give_to_teacher():
         lock.give(teacher)

#function calling
      give_to_student("Rickon")
      give_to_student("Carolina")
</code></pre>

<p>I hope this solves your purpose.</p>

python python-3.x