从 PyInstaller 包访问 Python 解释器 [英] Accessing Python interpreter from a PyInstaller bundle

查看:80
本文介绍了从 PyInstaller 包访问 Python 解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成 Pronsole.py(3D 打印)的程序(假设它被称为PROG").如果它只是由 Python 解释,它在 GNU/Linux 和 Windows 中运行良好.这是有效的行:

I have a program (suppose it is called "PROG") that spawn Pronsole.py (3D Printing). If it is just interpreted by Python, it works good in GNU/Linux and Windows. This is the line that works:

self.pronTranspProc=reactor.spawnProcess(self.pronProtProc, pythonPath, [pythonPath, "pronsole.py"], os.environ, self.pronPathPrintrun)

当 Python 是普通解释器时,pythonPath"将只是该解释器的路径,因为它是 sys.executable.但是当使用 Pyinstaller 制作捆绑包因此应用程序被冻结时,sys.executable 不是解释器,而是由 pyinstaller 生成的可执行文件(在本例中,它将是 PROG.EXE 而不是 Python.exe).该可执行文件嵌入了 python 解释器.问题是,如果我像 Python 解释器一样调用可执行文件,那么程序 PROG 再次打开,这是逻辑,并且不会与打印机进行通信.

When Python is the normal interpreter, "pythonPath" will be just the path to that interpreter, since it is sys.executable. But when a bundle is made with Pyinstaller so the app is frozen, sys.executable is not the interpreter, but the executable file which is generated by pyinstaller (in this example, it would be PROG.EXE instead of Python.exe). That executable has the python interpreter embedded. The problem is that if I call the executable as it if were the Python interpreter, then the program PROG opens again, which is logic, and no communication is made with the printer.

有没有办法调用内嵌的Python解释器?

Is there any way to call the embedded Python interpreter?

到目前为止,我所达到的解决方案是将 Python 解释器作为一个简单的文件添加到包中.但这是多余的,因为我知道解释器是嵌入的.有什么pythonic的方法吗?

Until now, the solution I have reached is adding the Python interpreter to the bundle, as a simple file. But this is redundant, since I know the interpreter is embedded. Any pythonic way of doing that?

任何帮助将不胜感激

推荐答案

试试这个:

exec(open('external_script.py').read())

解释器只能被冻结脚本引用,但是您可以使用 read 函数来执行其他脚本的主块.希望有帮助!

The interpreter can only be referred to by a frozen script, however you can use the read function to execute the main block of your other script. Hope that helps!

这篇关于从 PyInstaller 包访问 Python 解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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