从另一个文件更新导入的变量

时间:2019-05-18 21:41:50

标签: python python-3.x import python-import turtle-graphics

  1. 在文件A中,我从文件B导入了变量“ x”

  2. 我更改了文件A中的“ x”值

  3. 然后我覆盖文件B的“ x”声明。

现在,我需要在文件A中使用文件B中的新值“ x”。但是文件A并没有更新为文件B所获得的新值-它一直使用旧值(即不再存在)。如果我停止然后再次运行脚本,则文件A仅协调更改。

使用的真实姓名:

  • 导入的变量='vert'和'hor'。
  • 文件B ='blab'

源文件A:

from blab import *


nm = 'nome'
while nm != 'proximo' :
    from blab import *  # here_point
    import blab
    from blab import vert
    from blab import hor

    nm = str(input('Cadastrar - nome : '))
    pos = str(input(' Cadastrar - : %s . Ocupação : ' %(nm)))
    grp = str(input(' Cadastrar - : %s . Grupo : ' %(nm)))
    pos = pos.replace(" ", "_")
    grp = grp.replace(" ", "_")
    nm = nm.replace(" ","_")
    if nm != 'proximo' :
        if vert == -340:
            new_vert = 350
            new_hor = hor + 200
        if vert!= -340:
            new_vert= vert-30
            new_hor = hor
        txt_vert = new_vert - 15
        txt_hor = new_hor + 20
        with open("blab.py", "a") as fd:
            fd.write("\nt%s = turtle.Turtle(shape='turtle')\nt%s.color('pink')\ntext%s = turtle\ntext(text%s, '%s', '20', 'green', %s, %s)\nt%s.goto(%s, %s)" %(nm,nm,nm,nm,nm,txt_hor,txt_vert,nm,new_hor,new_vert))
            fd.write("\ndef t%s_handler(x, y):\n    pnt = screen.textinput(' pontuação', '%s:  ')" %(nm,nm))
            fd.write("\n    pnt = [int(x) for x in pnt.split()]\n    if len(pnt) == 5 :\n        with open('%s.py' %(mes), 'a') as fd:\n            fd.write('%s.pontuacao(%i,%i,%i,%i,%i)' % (pnt[0],pnt[1],pnt[2],pnt[3],pnt[4]))\n" )
            fd.write("        %s.color('blue')\n"%(nm))
            fd.write("t%s.onclick(t%s_handler)\n"%(nm,nm))
        print(vert,new_vert,hor,new_hor)
        with open('blab.py', 'r') as file:
            filedata = file.read()
            filedata = filedata.replace('vert=%i' %(vert),'vert=%i'  %(new_vert))
            with open('blab.py', 'w') as file:
                file.write(filedata)
                file.close()
        with open('blab.py', 'r') as file:
            filedatas = file.read()
            filedatas = filedatas.replace('hor=%.i'%(hor),'hor=%.i'%(new_hor))
            with open('blab.py', 'w') as file:
                file.write(filedatas)
                file.close()

源文件B:

import turtle
screen = turtle.Screen()

width = 1200
height = 1500
turtle.screensize(width, height)


def text(t,text, size, color, pos1, pos2):
    t.penup()
    t.goto(pos1, pos2)
    t.color(color)
    t.begin_fill()
    t.write(text, font=('Arial', size, 'normal'))
    t.end_fill()


vert=350
hor=-600

我试图在第7行重新导入变量,但这没有用。

注意:要运行一次脚本,只需在提示符下输入3个随机名称

1 个答案:

答案 0 :(得分:1)

这是您的示例的简化版本,它使用reload -可以满足您的目的:

from importlib import reload
import blab

nm = 'nome'

while nm != 'proximo':
    nm = str(input('Cadastrar - nome: '))

    if nm != 'proximo':
        new_vert = blab.vert - 100
        new_hor = blab.hor + 100

    print(blab.vert, new_vert, blab.hor, new_hor)

    with open('blab.py') as file:
        filedata = file.read()
        filedata = filedata.replace('vert=%i' % blab.vert, 'vert=%i' % new_vert)

    with open('blab.py', 'w') as file:
        file.write(filedata)

    with open('blab.py') as file:
        filedatas = file.read()
        filedatas = filedatas.replace('hor=%i' % blab.hor, 'hor=%i' % new_hor)

    with open('blab.py', 'w') as file:
        file.write(filedatas)

    reload(blab)

输出

> tail -2 blab.py 
vert=350
hor=-600
> python3 fileA.py
Cadastrar - nome: Larry
350 250 -600 -500
Cadastrar - nome: Moe
250 150 -500 -400
Cadastrar - nome: Shemp
150 50 -400 -300
Cadastrar - nome: proximo
50 50 -300 -300
> !tail
tail -2 blab.py
vert=50
hor=-300
>