我该如何分发python程序?

时间:2023-01-12 19:01:33

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.

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

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.

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

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:

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

  • The icon is the default python icon.
  • 该图标是默认的python图标。
  • 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.
  • 我必须手动将main.py重命名为main.pyw。
  • 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.
  • 如果只有`.pyo *文件在目标机器上,那就太好了。这没有真正的原因,我只是不喜欢有不必要的文件。

How does one create a nice automated distribution?

如何创建一个很好的自动化发行版?

  • for windows? (That's the only platform that I have to support at the moment.)
  • 对于Windows? (这是我目前唯一需要支持的平台。)
  • for mac?
  • 对于mac?
  • for linux?
  • 对于Linux?

6 个解决方案

#1


30  

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.

分发Python应用程序的常规方法是使用distutils。它既用于分发库类型python模块,也用于python应用程序,虽然我不知道它在Windows上是如何工作的。如果使用distutils,在Windows上你必须单独安装Python。

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.

我可能建议您使用disutils for Linux,Py2exe或类似的Windows进行分发。对于OS X,我不知道。如果它是最终用户应用程序,您可能想要一个磁盘映像类型的东西,我不知道该怎么做。但请阅读这篇文章,了解有关用户体验的更多信息。对于为程序员制作的应用程序,您可能也可以在OS X上安装distutils类型。

#2


52  

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.

我强烈推荐Pyinstaller,它可以无缝地支持所有主要平台。与py2exe和py2app一样,它在Windows上生成标准可执行文件,在OS X上生成应用程序包,但其优点还在于自动解决常见的依赖关系,并且无需额外的配置调整即可完成。

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

另请注意,如果要将Python 2.6部署到Windows,则应将此修补程序应用于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.

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

#3


5  

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!

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

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).

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

#4


2  

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

我可能弄错了,但是IronPython没有内置的windows编译器吗?

http://www.ironpython.net

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.

尝试Cx_Freeze,到目前为止,我用过的最好的.py到.exe(加上一些.dll)编译器。

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

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

#5


1  

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.

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

#6


0  

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:

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

  • 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.
  • Miniconda-Install - powershell / bash脚本,可以自动下载Miniconda并为应用程序创建独立的conda环境。支持点,但似乎没有维护,并有https下载问题。
  • 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.
  • Continuum的Anaconda项目和(conda)构造函数。两者都使用conda。 (conda)构造函数似乎能够在Windows上创建自包含安装程序甚至NSIS安装程序,但不支持pip。似乎表现得像Anaconda / Miniconda安装程序。
  • 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.
  • PyAppShare - 最终用户首先安装Miniconda / Anaconda(如运行时环境)。然后单个安装批处理/ bash脚本从yaml规范创建隔离的conda环境。该应用程序也是一个conda / pip包本身,它安装在环境中并创建一个可执行的入口点。自动创建跨平台桌面和程序快捷方式。他们激活环境并启动应用程序。支持点子。

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

*对开发者来说最方便。足够方便最终用户。

#1


30  

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.

分发Python应用程序的常规方法是使用distutils。它既用于分发库类型python模块,也用于python应用程序,虽然我不知道它在Windows上是如何工作的。如果使用distutils,在Windows上你必须单独安装Python。

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.

我可能建议您使用disutils for Linux,Py2exe或类似的Windows进行分发。对于OS X,我不知道。如果它是最终用户应用程序,您可能想要一个磁盘映像类型的东西,我不知道该怎么做。但请阅读这篇文章,了解有关用户体验的更多信息。对于为程序员制作的应用程序,您可能也可以在OS X上安装distutils类型。

#2


52  

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.

我强烈推荐Pyinstaller,它可以无缝地支持所有主要平台。与py2exe和py2app一样,它在Windows上生成标准可执行文件,在OS X上生成应用程序包,但其优点还在于自动解决常见的依赖关系,并且无需额外的配置调整即可完成。

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

另请注意,如果要将Python 2.6部署到Windows,则应将此修补程序应用于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.

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

#3


5  

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!

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

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).

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

#4


2  

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

我可能弄错了,但是IronPython没有内置的windows编译器吗?

http://www.ironpython.net

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.

尝试Cx_Freeze,到目前为止,我用过的最好的.py到.exe(加上一些.dll)编译器。

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

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

#5


1  

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.

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

#6


0  

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:

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

  • 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.
  • Miniconda-Install - powershell / bash脚本,可以自动下载Miniconda并为应用程序创建独立的conda环境。支持点,但似乎没有维护,并有https下载问题。
  • 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.
  • Continuum的Anaconda项目和(conda)构造函数。两者都使用conda。 (conda)构造函数似乎能够在Windows上创建自包含安装程序甚至NSIS安装程序,但不支持pip。似乎表现得像Anaconda / Miniconda安装程序。
  • 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.
  • PyAppShare - 最终用户首先安装Miniconda / Anaconda(如运行时环境)。然后单个安装批处理/ bash脚本从yaml规范创建隔离的conda环境。该应用程序也是一个conda / pip包本身,它安装在环境中并创建一个可执行的入口点。自动创建跨平台桌面和程序快捷方式。他们激活环境并启动应用程序。支持点子。

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

*对开发者来说最方便。足够方便最终用户。