使用py2exe打包的python解释器
我有一个 python 脚本想要分发到 Windows,其中人们可能没有安装 python。所以我使用py2exe。问题出在脚本中,我使用子进程运行其他Python脚本,这需要Python解释器作为要执行的程序。由于我没有在 Windows 上安装 python 解释器,有什么方法可以忽略解释器并解决该问题?有什么方法可以调用 py2exe 的 python 解释器 pakcagged 吗?
I have a python script I want to distribute to Windows, where people might not have python installed. So I use py2exe. The problem is in the script I run other python scripts by using subprocess, which requires python interpreter as the program to execute. As I don't have python interpreter installed on Windows, is there any way I could ignore the interpreter and work around the problem? Is there any way I could call the python interpreter pakcaged by py2exe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它可能比您想象的更简单:使用内置的
eval()
命令来执行脚本,而不是启动子进程。[编辑] 要重定向 stdio,请替换 sys.stdout/ sys.stderr 带有缓冲区或其他支持“write()”的东西。
要恢复原始值,
sys
模块提供__stdout__
等。[EDIT2] 我还没有尝试过,但它可能有效:添加“python.exe”到 py2exe 创建的文件集。
从主代码中,将 py2exe 创建的所有文件 + python.exe 复制到临时目录中。然后添加所有脚本。
现在使用一个小脚本启动新的 python 解释器,该脚本将临时文件夹和library.zip 添加到 sys.path
注意:不必“安装”Python就像 Windows 应用程序一样。事实上,您只需将所有文件复制到新位置即可。只要搜索路径正确,就可以。
It's probably more simple than you think: Instead of starting sub-processes, use the built-in
eval()
command to execute the scripts.[EDIT] To redirect stdio, replace sys.stdout/sys.stderr with buffers or something else that supports "write()".
To restore the original values, the
sys
module offers__stdout__
, etc.[EDIT2] I haven't tried this but it might work: Add "python.exe" to the set of files which py2exe creates.
From the main code, copy all files that py2exe created + the python.exe into a temporary directory. Then add all your scripts.
Now start the new python interpreter with a small script that adds the temp folder and
library.zip
to thesys.path
Note: Python doesn't have to be "installed" like a Windows application. In fact, you can simply copy all the files to a new place. As long as the search path is correct, this works.