脚本编译后如何在python中重新加载模块? [英] How to make a module reload in python after the script is compiled?

查看:102
本文介绍了脚本编译后如何在python中重新加载模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个学生可以编写代码的应用程序与特定问题相关(比如检查数字是否为偶数)学生给出的代码然后由应用程序检查将用户代码给出的输出与正确的输出进行比较由应用程序中已经存在的正确代码给出.

I am trying to make an application where students can write code related to a specific problem(say to check if the number is even) The code given by the student is then checked by the application by comparing the output given by the user's code with the correct output given by the correct code which is already present in the application.

您可以在其中编写 python 脚本的应用程序(在 tkinter 文本中盒子).文本框的内容首先存储在一个test_it.py文件.然后这个文件被导入(点击一个按钮)应用.然后调用 test_it.py 中的函数获取代码的输出(由用户).

An application in which you can write a python script (in tkinter text box). The contents of the text box are first stored in a test_it.py file. This file is then imported (on the click of a button) by the application. The function present in test_it.py is then called to get the output of the code(by the user).

由于我正在导入"test_it.py 的内容,因此,在应用程序运行期间,用户可以测试他的脚本只有一次.原因是python会导入test_it.py只存档一次.所以即使在保存用户的新脚本之后test_it.py ,它不会被应用程序使用.

Since I am "importing" the contents of test_it.py , therefore, during the runtime of the application the user can test his script only once. The reason is that python will import the test_it.py file only once. So even after saving the new script of the user in test_it.py , it wont be available to the application.

Reload test_it.py 每次点击测试脚本的按钮时.

Reload test_it.py every time when the button to test the script is clicked.

虽然当我从脚本运行应用程序时这很完美,此方法对文件的已编译/可执行版本 (.exe) 不起作用(这是预期的,因为在编译期间所有导入的模块都将编译过,所以以后修改它们将不起作用)

While this works perfectly when I run the application from the script, this method fails to work for the compiled/executable version(.exe) of the file (which is expected since during compilation all the imported modules would be compiled too and so modifying them later will not work)

我希望即使在编译应用程序之后重新加载我的 test_it.py 文件.

I want my test_it.py file to be reloaded even after compiling the application.

如果您想查看应用程序的工作版本以自行测试.您可以在此处找到它.

If you would like to see the working version of the application to test it yourself. You will find it here.

推荐答案

即使对于捆绑的应用程序导入也以标准方式工作.这意味着每当遇到 import 时,解释器都会尝试找到相应的模块.您可以通过将包含目录附加到 test_it.py 模块可被发现="nofollow noreferrer">sys.path.import test_it 应该是动态的,例如在函数内部,这样它就不会被 PyInstaller 发现(这样 PyInstaller 就不会尝试将它与应用程序捆绑在一起).

Even for the bundled application imports work the standard way. That means whenever an import is encountered, the interpreter will try to find the corresponding module. You can make your test_it.py module discoverable by appending the containing directory to sys.path. The import test_it should be dynamic, e.g. inside a function, so that it won't be discovered by PyInstaller (so that PyInstaller won't make an attempt to bundle it with the application).

考虑以下示例脚本,其中应用数据存储在托管 test_it.py 模块的临时目录中:

Consider the following example script, where the app data is stored inside a temporary directory which hosts the test_it.py module:

import importlib
import os
import sys
import tempfile

def main():
    with tempfile.TemporaryDirectory() as td:
        f_name = os.path.join(td, 'test_it.py')

        with open(f_name, 'w') as fh:  # write the code
            fh.write('foo = 1')

        sys.path.append(td)  # make available for import
        import test_it
        print(f'{test_it.foo=}')

        with open(f_name, 'w') as fh:  # update the code
            fh.write('foo = 2')

        importlib.reload(test_it)
        print(f'{test_it.foo=}')

main()

这篇关于脚本编译后如何在python中重新加载模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆