如何在没有 Visual Studios(也可能使用 py2exe)的情况下将 IronPython 应用程序编译为 EXE?

发布于 2024-12-16 19:02:54 字数 584 浏览 0 评论 0原文

所以我在直接来这里询问之前尝试寻找这个答案。将 IronPython 应用程序/脚本集转换为 EXE 似乎绝对是可行的。一个问题是,他们似乎都在谈论将 IronPython 与 IronPython Studios(一种 Visual Studio 的 Python 插件)一起使用。最后我检查了一下,我认为 IronPython Studios 基本上已被 Python Tools for Visual Studios(或 PTVS)插件工具所取代。问题在于,由于我的代码的结构方式,在将我的代码加载到 IDE 中时,它实际上设法使插件崩溃,从而使 Visual Studio 崩溃。它将持续到本月底或下月初发布下一个计划更新为止。因此,这不会是一个选项,因为我必须在 Eclipse 中开发我的项目,而不是使用 PyDev 插件。

我之前曾使用 py2exe 将一些基本的 Python 应用程序转换为可执行文件,但那是当我使用 CPython 解释器时。我不知道如何设置 py2exe 来与 IronPython 一起使用。是否可以使用 py2exe 制作 IronPython 可执行文件?如果我的程序使用额外的 C# 程序集来完成其工作,我将如何设置它们以便 py2exe 可以使用它们并且我的可执行文件可以看到它们以供引用?

So I tried searching for this answer before coming straight here to ask it. It definitely seems doable to turn an IronPython app/set of scripts into an EXE. The one issue is that they all seem to talk about using IronPython with IronPython Studios, a Python plugin for Visual Studios. Last I checked, I thought that IronPython Studios was basically replaced with the Python Tools for Visual Studios (or PTVS) plugin tool. The problem with this being that due to the way my code is structured, it actually managed to crash the plugin, and therefore Visual Studios, upon loading my code into the IDE. It will do so until the next scheduled update coming out at the end of the month or early next month. So that is not going to be an option as I've been having to develop my project in Eclipse instead with the PyDev plugin.

I have used py2exe to convert some basic Python applications into executables before, but that was when I was using the CPython interpreter. I don't know how I would set up py2exe to work with IronPython. Would it be possible to use py2exe to make an IronPython executable? And if my program uses extra C# assemblies to do its work, how would I set those up too so py2exe can use them and my executable can see them for referencing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2024-12-23 19:02:54

IronPython 附带一个命令行编译器 pyc.py(位于 Tools\Scripts 文件夹中)你可以使用它。另一种选择是使用 SharpDevelop 它将您的 IronPython 代码编译为可执行文件。

如果您编译的 IronPython 应用程序位于 GAC 中或者与可执行文件一起部署在同一文件夹中,则它们可以使用 C# 程序集。

IronPython ships with a command line compiler pyc.py (in the Tools\Scripts folder) which you could use. Another alternative is to use SharpDevelop which will compile your IronPython code to an executable.

Your compiled IronPython application can use C# assemblies if they are in the GAC or if they are deployed alongside your executable in the same folder.

过去的过去 2024-12-23 19:02:54

我使用 IronPyton 2.7.8 和 ipyc.exe,而不是 Tools\Scripts\pyc.py,它引入了一些差异,所以我在这里留下一些信息。

1.使用 /embed 和 /standalone 选项

如果您将脚本拆分为多个 py 文件,然后使用 /embed 选项和 /main 选项,它们将生成一个包含所有 py 脚本文件的 exe 文件。
例如,您有“MyMain.py”脚本和“OtherFuncs.py”,如下所示。

C:\Program Files\IronPython 2.7\ipyc.exe /main:MyMain.py OtherFuncs.py /embed /platform:x86 /standalone /target:winexe

/standalone 选项用于包含基本 dll 文件对于 C:\Program Files\IronPython 2.7\ 文件夹下的 IronPython,如 IronPython.dll、IronPython.Modules.dll、Microsoft.Dynamic.dll 和 Microsoft.Scripting.dll

但请注意,DLLs/IronPyton.SQLite.dll 和 DLLs/IronPtyhon.WPF.dll 不包括在内,因此,如果您在脚本中使用 WPF 或 SQLite 功能,则应将这些 dll 文件与 exe 文件一起复制和分发,并且您应该在脚本中添加以下行

clr.AddReference("IronPython.Wpf")
clr.AddReference("IronPython.SQLite")

2。 C:\Program Files\IronPython 2.7\Lib 文件夹下的库脚本文件

您的脚本使用 C:\Program Files\IronPython 2.7\Lib 文件夹下的库脚本文件支持的许多标准函数,因此您应该分发这些脚本文件与您的 exe 一起使用。

有两种选择,一种是将 Lib 文件夹复制到包含 exe 文件的文件夹,另一种是按照以下命令制作包含所有标准库脚本文件的 StdLib.dll。

C:\Program Files\IronPython 2.7\ipyc.exe /main:StdLib.py io.py sys.py ...所有必需的脚本文件... /embed /platform:x86 /target:dll

StdLib.py 只是大小为 0 的空文件,它是生成单个 dll 文件的 hack。并且您应该在脚本的第一行添加以下行。

import clr
clr.AddReference("StdLib")

我使用 python 脚本制作 StdLib.dll ,如下所示。

#...
#Build StdLib.DLL
ipath = 'C:\Program Files\IronPython 2.7'
ipyc = ipath + '\ipyc.exe'
# any library files you need
gb += glob.glob(r".\Lib\*.py")
gb += glob.glob(r".\Lib\encodings\*.py")
# ...
gb = [ipyc,"/main:StdLib.py","/embed","/platform:x86","/target:dll"] + gb
subprocess.call(gb)
print "Made StdLib"

I use IronPyton 2.7.8 and ipyc.exe rather than Tools\Scripts\pyc.py, and it introduces some differences, so i am leaving some information here.

1. use /embed and /standalone option

If you split your script into more than one py files, then use /embed option with /main option, they will generate a single exe file including all py script files.
For example you have "MyMain.py" script and "OtherFuncs.py" then as follows.

C:\Program Files\IronPython 2.7\ipyc.exe /main:MyMain.py OtherFuncs.py /embed /platform:x86 /standalone /target:winexe

/standalone option is used for including basic dll files for IronPython under the C:\Program Files\IronPython 2.7\ folder like IronPython.dll, IronPython.Modules.dll, Microsoft.Dynamic.dll, and Microsoft.Scripting.dll

But be careful that DLLs/IronPyton.SQLite.dll and DLLs/IronPtyhon.WPF.dll are not included, so if you use WPF or SQLite functionality in your script, you should copy and distribute these dll files additionally with your exe file, and you should add the following line in your script

clr.AddReference("IronPython.Wpf")
clr.AddReference("IronPython.SQLite")

2. libraries script files under the C:\Program Files\IronPython 2.7\Lib folder

your script use many standard functions supported by the library script files under the C:\Program Files\IronPython 2.7\Lib folder, so you should distribute these script files with your exe.

there are two options, one is just copying the Lib folder to the folder having your exe file, and another is making a StdLib.dll having all standard library script files as the following command.

C:\Program Files\IronPython 2.7\ipyc.exe /main:StdLib.py io.py sys.py ...all necessary script files... /embed /platform:x86 /target:dll

StdLib.py is just empty file with 0 size, it is a hack for generating a single dll file. and you should add the following lines at the very first line in your script.

import clr
clr.AddReference("StdLib")

I use a python script making StdLib.dll something like the following.

#...
#Build StdLib.DLL
ipath = 'C:\Program Files\IronPython 2.7'
ipyc = ipath + '\ipyc.exe'
# any library files you need
gb += glob.glob(r".\Lib\*.py")
gb += glob.glob(r".\Lib\encodings\*.py")
# ...
gb = [ipyc,"/main:StdLib.py","/embed","/platform:x86","/target:dll"] + gb
subprocess.call(gb)
print "Made StdLib"
べ繥欢鉨o。 2024-12-23 19:02:54

Pyc.py 是可行的方法,因为 Visual Studio 的 Python 工具不支持直接编译。 SharpDevelop 具有适用于 Ironpython 的自定义构建操作。在 PTVS 出现之前我一直使用 #Develop。

看到这个问题IronPython:使用编译的EXE pyc.py 无法导入模块“os”,了解制作 EXE 时需要遵循的一些详细步骤。请记住编译您正在使用的标准库模块。如果您必须让用户下载并安装 IronPython,那么您就不需要编译脚本。

Pyc.py is the way to go as Python Tools for Visual Studio does not support direct compilation. SharpDevelop has custom build actions for Ironpython. I used #Develop until PTVS came out.

See this question IronPython: EXE compiled using pyc.py cannot import module "os" for some detailed steps to follow to make your EXE. Remember to compile the standard lib modules you are using. If you have to make your users download and install IronPython then you wouldn't need to compile your script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文