eiffeldreams eiffeldreams - 2 months ago 8
Python Question

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)?

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.

This is the runme file. That I'm running.

command > python runme.py

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")


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 (,).

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


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.

a = "asb12"
b = "121002"
c = "mya122344"


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.

and function definition of give_to_student and give_to_teacher which uses these function definitions.

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)


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.

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

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

#Added it in runme.py file

import importlib
importlib.reload(mainfile)


It is still giving authorization for first line of data(schooldata.txt).

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

#added it in mainfile.py

import importlib
importlib.reload(school_info)


importlib.reload(school_info)
NameError: name 'school_info' is not defined

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.S. I'm using python 3.5. Thanks

Answer

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

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")

I hope this solves your purpose.

Comments