【问题标题】:Alternatives to locals() for Pyinstaller interactive consolePyinstaller 交互式控制台的 locals() 替代品
【发布时间】:2020-10-12 13:04:46
【问题描述】:

目前我正在使用 Pyinstaller 捆绑我拥有的大量 python 脚本并将它们放入可执行文件中。

可执行文件的主要入口点和一般运行软件使用

import code
... Some functions here ...
def main():
    code.interact(local=locals())

通过 python -m path.script.py 或我在 setup.py, script 中的入口点调用它时运行没有问题。

现在的主要问题是 Pyinstaller 发挥了它的魔力,而 locals() 不再起作用,因为它失去了在以前的方式运行时保留的原始值。由于这个原因,现在我的任何函数都无法在控制台中调用,以前是。

有什么想法吗?

编辑:Pyinstaller 命令pyinstaller -F -p src/path/ src/path/script.py

【问题讨论】:

    标签: python python-3.x pyinstaller interactive


    【解决方案1】:

    我最终找到了一个适用于 globals() 的解决方案。

    关键是让函数 main 成为脚本的入口点,而不是 locals(),换成 globals()。

    对于 Pyinstaller,code.interact 必须位于 __main__ 内,以便 Pyinstaller 正确关联位于 script.py 中的所有函数。

    【讨论】:

      猜你喜欢
      • 2013-08-12
      • 2012-08-20
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2012-06-24
      • 2014-10-21
      • 1970-01-01
      相关资源
      最近更新 更多