如何让python脚本(X)重新加载动态更改另一个模块(Y)中的变量,然后在同一个脚本(X)中重新导入更新的模块(Y)?

时间:2016-09-12 08:03:01

标签: python python-3.x

我是Python的新手,我坚持使用代码。我已尽力用下面的示例代码显示我的问题。我正在玩4个文件。

这是runme文件。我正在跑步。

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

第二个文件是schooldata.txt,我想从中读取a,b,c的值。这是我们的主要学校数据文件,我们从中获取授权数据。我从这个文件中逐行阅读,然后用(,)分割它来创建a,b,c。

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

第三个文件是school_info.py,我每次都使用这些数据创建。每次从schooldata.txt文件中读取一行时都会创建此文件。每次都有新鲜的文件,包含a,b,c的新鲜独特数据。

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

现在这里有mainfile.py,它有像give_to_student和give_to_teacher这样的函数。此文件从school_info.py导入数据,以便使用a,b,c的值创建授权代码。

以及使用这些函数定义的give_to_student和give_to_teacher的函数定义。

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)

现在让我分享一下我到目前为止面临的确切问题,每次在runme.py文件中导入时,我都无法获得为mainfile.py加载的授权代码。当我调用runme.py文件时,它每次都向所有用户提供相同的授权码。

无法使用在读取schooldata.txt第二行后创建的授权码

使用mainfile.py文件如果我正在尝试使用重新加载模块。在runme.py中导入importlib,然后导入importlib.reload(mainfile.py)。

#Added it in runme.py file

import importlib
importlib.reload(mainfile)

它仍在授权第一行数据(schooldata.txt)。

我在mainfile.py中尝试过类似的事情。 我尝试导入importlib,然后导入importlib.reload(school_info)。

#added it in mainfile.py

import importlib
importlib.reload(school_info)

importlib.reload(school_info) NameError:name' school_info'未定义

但它给出了错误,即school_info模块并不存在。 请详细说明一下,我怎样才能使它发挥作用。

P.S。我使用的是python 3.5。感谢

1 个答案:

答案 0 :(得分:0)

为什么不尝试将school_info.py和mainfile.py结合起来。 如果你可以运行一个组合循环。

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

我希望这能解决你的目的。