简体   繁体   English

Python如何重新加载.py文件而不是.pyc文件?

[英]Python how do I reload the .py file not the .pyc file?

I'm making a script in python that writes code to a .py file in the fourm of variables (eg x = 6). 我正在用python编写一个脚本,该脚本使用变量的4个变量(例如x = 6)将代码写入.py文件。 The problem is when I import it to use the variables and then write some code to it I need to use reload() to refresh the import I quess and allow me to use the variable but it wont reload the .py file it will only reload the .pyc file. 问题是,当我导入它以使用变量,然后向其中写入一些代码时,我需要使用reload()刷新我查询的导入并允许我使用该变量,但它不会重新加载.py文件,它只会重新加载.pyc文件。

Heres some code to help (not so sure it will :/) 这是一些代码可以提供帮助(不确定是否会:/)

I call a function that writes the data to the .py file and then calls a function to reloading the module but it ends up reload the .pyc 我调用了一个将数据写入.py文件的函数,然后调用了一个函数来重新加载模块,但最终却重新加载了.pyc

make(2, "name", "'Bob'")

def re(module):
        reload(module)
        print"Reloaded "+ str(module)


def make(0-1-2, var, data):
    if 0-1-2 == 1:
        TempF.write(var+" = "+data)
        TempF.write("\n")
        re(Temp)

    if 0-1-2 == 2:
        PermF.write(var+" = "+data)
        PermF.write("\n")
        re(Perm)

    if 0-1-2 == 0:
        MiscF.write(var+" = "+data)
        MiscF.write("\n")
        re(Misc)


    Reloaded <module 'Perm' from 'C:\Users\Admin\PycharmProjects\DeanRobbieRowe\Perm.pyc'>

Try using the file object's .flush() method after writing. 编写后尝试使用文件对象的.flush()方法。

So: 所以:

make(2, "name", "'Bob'")

def re(module):
        reload(module)
        print"Reloaded "+ str(module)


def make(0-1-2, var, data):
    if 0-1-2 == 1:
        TempF.write(var+" = "+data)
        TempF.write("\n")
        TempF.flush()
        re(Temp)

    if 0-1-2 == 2:
        PermF.write(var+" = "+data)
        PermF.write("\n")
        PermF.flush()
        re(Perm)

    if 0-1-2 == 0:
        MiscF.write(var+" = "+data)
        MiscF.write("\n")
        MiscF.flush()
        re(Misc)

See: what exactly the python's file.flush() is doing? 请参阅: python的file.flush()到底在做什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM