问题:如何分发python程序?

我的应用程序如下所示:

main.py
视窗/
    __init__.py
    mainwindow.py
    ...
模型/
    __init__.py
    orders.py
    ...
资源/
    image1.png
    logo.jpg
    ...

该程序从main.py开始。有没有一种好的方法可以从中创建“最终”应用程序?我在想像py2exe / py2app之类的东西,但是没有将python解释器/模块复制到只有一个可执行文件的应用程序中。

我看过distutils,但这看起来像是将程序安装到Python目录中,这在非Linux平台上并不常见。

目前,我只是将整个源文件夹复制到目标计算机上,并main.pyw在Windows上创建一个别名。不便之处:

  • 该图标是默认的python图标。
  • 我必须手动创建别名。
  • 在我的源目录中,还有许多其他文件,例如源代码管理文件夹。
  • 我必须重命名main.pymain.pyw手动。
  • 如果目标机器上只有`.pyo *文件,那会很好。没有真正的理由,我只是不喜欢拥有不必要的文件。

一个人如何创建一个不错的自动化发行版?

  • 对于Windows?(这是我目前必须支持的唯一平台。)
  • 对于Mac?
  • 对于Linux?

My application looks like this:

main.py
windows/
    __init__.py
    mainwindow.py
    ...
model/
    __init__.py
    orders.py
    ...
resources/
    image1.png
    logo.jpg
    ...

The program is started with main.py. Is there a good way to create a ‘final’ application out of it? I’m thinking of something like py2exe/py2app, but without copying the python interpreter / modules into the application where one has only one executable.

I had a look at distutils, but this looks like it installs a program into the Python directory, which isn’t usual on non-linux platforms.

At the moment I just copy the whole source folder onto the target machine and create an alias to main.pyw on windows. Some inconveniences:

  • The icon is the default python icon.
  • I have to create the alias manually.
  • In my source directory there are a lot of additional files like the source control folder.
  • I have to rename main.py to main.pyw manually.
  • It would be nice if only `.pyo* files are on the target machine. There’s no real reason for it, I just don’t like having unnecessary files.

How does one create a nice automated distribution?

  • for windows? (That’s the only platform that I have to support at the moment.)
  • for mac?
  • for linux?

回答 0

分发Python应用程序的常规方法是使用distutils。它既用于分发库类型的python模块,又用于python应用程序,尽管我不知道它在Windows上如何工作。在任何情况下,如果您使用distutils,则必须在Windows上分别安装Python。

我可能会建议您使用Linux的disutils和Windows的Py2exe或类似的软件分发它。对于OS XI不知道。如果它是最终用户应用程序,则可能需要磁盘映像类型的东西,我不知道该怎么做。但是,请阅读这篇文章以获取有关其用户体验的更多信息。对于为程序员开发的应用程序,您也可以在OS X上安装distutils类型。

The normal way of distributing Python applications is with distutils. It’s made both for distributing library type python modules, and python applications, although I don’t know how it works on Windows. You would on Windows have to install Python separately if you use distutils, in any case.

I’d probably recommend that you distribute it with disutils for Linux, and Py2exe or something similar for Windows. For OS X I don’t know. If it’s an end user application you would probably want an disk image type of thing, I don’t know how to do that. But read this post for more information on the user experience of it. For an application made for programmers you are probably OK with a distutils type install on OS X too.


回答 1

我强烈推荐Pyinstaller,它可以无缝地支持所有主要平台。像py2exe和py2app一样,它在Windows上生成标准可执行文件,在OS X上生成应用程序捆绑包,但它的好处是还可以出色地完成自动解决常见依赖项的工作,并且无需进行额外的配置调整即可包含它们。

还要注意,如果要将Python 2.6部署到Windows,则应将此补丁应用到Pyinstaller干线。

您表示不需要安装程序,但是Inno Setup是Windows平台上易于使用和快速设置的选择。

I highly recommend Pyinstaller, which supports all major platforms pretty seamlessly. Like py2exe and py2app, it produces a standard executable on Windows and an app bundle on OS X, but has the benefit of also doing a fantastic job of auto-resolving common dependencies and including them without extra configuration tweaks.

Also note that if you’re deploying Python 2.6 to Windows, you should apply this patch to Pyinstaller trunk.

You indicated that you don’t need an installer, but Inno Setup is an easy to use and quick to setup choice for the Windows platform.


回答 2

Fredrik Lundh squeeze.py可以创建一个不包含Python解释器但包含字节码的文件。使用正确的参数,您可以在结果文件中包括其他文件,模块等。我在一个项目中成功使用了它。生成的程序可以在OS X,Linux和Windows上运行,没有任何问题!

PS:每台机器都需要有一个与squeeze.py生成的字节码兼容的Python解释器。如果需要,可以为不同版本的Python生成不同的字节码版本(只需使用正确的Python版本运行squeeze.py)。

Fredrik Lundh’s squeeze.py can create a single file that does not contain the Python interpreter, but instead contains bytecode. With the right arguments, you can include other files, modules, etc. in the result file. I used it successfully in one project. The resulting program ran on OS X, Linux and Windows without any problem!

PS: Each machine needs to have a Python interpreter which is compatible with the bytecode generated by squeeze.py. You can generate different bytecode versions for different versions of Python, if need be (just run squeeze.py with the right version of Python).


回答 3

我认为也值得一提的是PEX(多考虑这个问题引起的关注,而不是问题本身)。根据自己的描述

PEX文件是自包含的可执行Python虚拟环境。更具体地说,它们是精心构造的zip文件,带有#!/usr/bin/env python__main__.py,允许您与PEX运行时进行交互。有关zip应用程序的更多信息,请参阅PEP 441

当我阅读python包装概述时偶然发现了它。他们在这里张贴了这张漂亮的照片: 在此处输入图片说明

总结一下:如果您可以负担得起依靠目标计算机上安装的python的功能,请使用PEX生成自包含的“可执行文件”,例如,文件大小可能比PyInstaller生成的可执行文件小。

I think it’s also worth mentioning PEX (considering more the attention this question received and less the question itself). According to its own description:

PEX files are self-contained executable Python virtual environments. More specifically, they are carefully constructed zip files with a #!/usr/bin/env python and special __main__.py that allows you to interact with the PEX runtime. For more information about zip applications, see PEP 441.

I stumbled upon it when I read an overview of packaging for python. They posted this nice picture there: enter image description here

To summarize: If you can afford relying on python being installed on the target machine, use PEX to produce a self-containing »executable« which probably will have smaller file size than an executable produced by PyInstaller, for example.


回答 4

我可能会弄错了,但是IronPython是否没有针对Windows的内置编译器?

http://www.ironpython.net

[编辑]

试用Cx_Freeze,这是迄今为止我用过的最好的.py到.exe(加上一些.dll)编译器。

http://cx-freeze.sourceforge.net/

I may be mistaken, but doesn’t IronPython have a built in compiler for windows?

http://www.ironpython.net

[EDIT]

Try out Cx_Freeze, By far the best .py to .exe (plus a few .dlls) compiler I’ve ever used.

http://cx-freeze.sourceforge.net/


回答 5

如果要在Windows上分发,请使用安装程序来安装所有需要的所有相关文件/网络。分发setup.exe。这是在Windows上的最佳方法。否则,用户会抱怨。

If you are distributing on windows, use an installer to install all the relevant files/interpeter whatever is needed. Distribute a setup.exe. That is the best way on windows. Otherwise users will complain.


回答 6

分发python桌面应用程序最方便的跨平台方法是依靠跨平台的conda软件包管理器。有几种使用它的工具:

  • Miniconda安装-Powershell / bash脚本,可自动下载Miniconda并为应用创建隔离的conda环境。支持pip,但似乎无法维护,并且具有https下载问题。
  • Anaconda项目和Continuum的(conda)构造函数。两者都使用conda。(conda)构造函数似乎能够在Windows上创建自包含的安装程序,甚至NSIS安装程序,但不支持pip。看起来像Anaconda / Miniconda安装程序。
  • PyAppShare-最终用户首先安装Miniconda / Anaconda(类似于运行时环境)。然后,单次安装批处理/ bash脚本从yaml规范创建隔离的conda环境。该应用程序本身也是一个conda / pip程序包,已安装到环境中并创建了一个可执行的入口点。自动创建跨平台的“桌面和程序”快捷方式。他们激活环境并启动应用程序。支持点子。

*对开发人员最方便。对于最终用户来说足够方便。

The most convenient* cross-platform way of distributing python desktop applications is to rely on cross-platform conda package manager. There are several tools that use it:

  • Miniconda-Install – powershell/bash scripts that auto-download Miniconda and create isolated conda environment for the app. Supports pip but seem to be unmaintained and has https download issues.
  • Anaconda Project and (conda) constructor by Continuum. Both use conda. (conda) constructor seem to be able to create self-contained installers and even NSIS installer on Windows but doesn’t support pip. Seem to behave like Anaconda/Miniconda installers.
  • PyAppShare – the end-user installs Miniconda/Anaconda first (like a runtime environment). Then single install batch/bash script creates isolated conda environment from yaml spec. The application is also a conda/pip package itself that is installed to environment and an executable entry point is created. Cross-platform Desktop and Programs shortcuts automatically created. They activate environment and start the application. Supports pip.

* The most convenient for the developer. Enough convenient for the end-user.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。