如何使Python脚本独立可执行文件在没有任何依赖的情况下运行?

问题:如何使Python脚本独立可执行文件在没有任何依赖的情况下运行?

我正在构建一个Python应用程序,不想强制我的客户端安装Python和模块。

因此,有没有办法将Python脚本编译为独立的可执行文件?

I’m building a Python application and don’t want to force my clients to install Python and modules.

So, is there a way to compile a Python script to be a standalone executable?


回答 0

您可以使用已经回答的py2exe并使用cython来转换C编译文件中的密钥.py文件.pyc,例如.dll在Windows和.solinux中,它比普通文件.pyo.pyc文件要难得多(并且还可以提高性能!)

You can use py2exe as already answered and use cython to convert your key .py files in .pyc, C compiled files, like .dll in Windows and .so in linux, much harder to revert than common .pyo and .pyc files (and also gain in performance!)


回答 1

您可以使用PyInstaller将Python程序打包为独立的可执行文件。它适用于Windows,Linux和Mac。

PyInstaller快速入门

从PyPI安装PyInstaller:

pip install pyinstaller

转到程序的目录并运行:

pyinstaller yourprogram.py

这将在名为的子目录中生成捆绑包dist

有关更详细的演练,请参见手册

You can use PyInstaller to package Python programs as standalone executables. It works on Windows, Linux, and Mac.

PyInstaller Quickstart

Install PyInstaller from PyPI:

pip install pyinstaller

Go to your program’s directory and run:

pyinstaller yourprogram.py

This will generate the bundle in a subdirectory called dist.

For a more detailed walkthrough, see the manual.


回答 2

您可能希望调查 Nuitka。它需要python源代码并将其转换为C ++ API调用。然后将其编译为可执行二进制文件(在Linux上为ELF)。它已经存在了几年,并且支持多种Python版本。

如果使用它,您可能还会获得性能上的改进。推荐的。

You might wish to investigate Nuitka. It takes python source code and converts it in to C++ API calls. Then it compiles into an executable binary (ELF on Linux). It has been around for a few years now and supports a wide range of Python versions.

You will probably also get a performance improvement if you use it. Recommended.


回答 3

我想汇编一些有关使用Python 2.7在Windows上创建独立文件的有用信息。

我已经使用了py2exe,并且可以使用,但是我遇到了一些问题。

这最后一个原因使我尝试使用PyInstaller http://www.pyinstaller.org/

我认为,这样做更好,因为:

  • 它更易于使用。

我建议例如使用以下几行创建一个.bat文件(pyinstaller.exe必须在Windows路径中):

pyinstaller.exe --onefile MyCode.py

因此,我认为,至少对于python 2.7,更好和更简单的选择是PyInstaller。

I would like to compile some useful information about creating standalone files on windows using Python 2.7.

I have used py2exe and it works, but I had some problems.

This last reason made me try PyInstaller http://www.pyinstaller.org/ .

In my opinion, it is much better because:

  • It is easier to use.

I suggest creating a .bat file with the following lines for example (pyinstaller.exe must be in Windows Path):

pyinstaller.exe --onefile MyCode.py

So, I think that, at least for python 2.7, a better and simpler option is PyInstaller.


回答 4

是的,可以将Python脚本编译成独立的可执行文件。

WindowsLinuxMac OS XFreeBSDSolarisAIX下,可以使用PyInstaller将Python程序转换为独立的可执行文件。它是推荐的转换器之一。

py2exe将Python脚本转换为仅Windows平台上的可执行文件。

Cython是用于Python编程语言和扩展的Cython编程语言的静态编译器。

Yes, it is possible to compile Python scripts into standalone executable.

PyInstaller can be used to convert Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX. It is one of the recommended converters.

py2exe converts Python scripts into only executable in Windows platform.

Cython is a static compiler for both the Python programming language and the extended Cython programming language.


回答 5

第三个选择是cx_Freeze,它是跨平台的。

And a third option is cx_Freeze, which is cross-platform.


回答 6

您可能喜欢py2exe。您还将在那里找到在Linux上进行此操作的信息

you may like py2exe. you’ll also find in there infos for doing it on linux


回答 7

使用py2exe…。使用以下设置文件:

 from distutils.core import setup
 import py2exe

 from distutils.filelist import findall
 import matplotlib

 setup(
       console=['PlotMemInfo.py'],

       options={
                'py2exe': {
                'packages' : ['matplotlib'],
            'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll',
                 'libgdk_pixbuf-2.0-0.dll']
                          }
                },
       data_files = matplotlib.get_py2exe_datafiles()
     )

Use py2exe…. use below set up files:

 from distutils.core import setup
 import py2exe

 from distutils.filelist import findall
 import matplotlib

 setup(
       console=['PlotMemInfo.py'],

       options={
                'py2exe': {
                'packages' : ['matplotlib'],
            'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll',
                 'libgdk_pixbuf-2.0-0.dll']
                          }
                },
       data_files = matplotlib.get_py2exe_datafiles()
     )

回答 8

我喜欢pyinstaller-尤其是“窗口式”变体:

pyinstaller --onefile --windowed myscript.py

它将在dist /文件夹中创建一个单个* .exe文件。

I like pyinstaller – especially the “windowed” variant:

pyinstaller --onefile --windowed myscript.py

It will create one single *.exe file in a dist/ folder.


回答 9

我还建议使用 pyinstaller 以获得更好的向后兼容性,例如python 2.3-2.7
对于py2exe,您必须具有python 2.6

I also recommend pyinstaller for better backward compatibility such as python 2.3 – 2.7.
for py2exe, you have to have python 2.6


回答 10

对于Python 3.2脚本,唯一的选择是Cxfreeze。从源代码构建它,否则它将不起作用。

对于python 2.x,我建议使用pyinstaller,因为它可以将python程序打包在单个可执行文件中,这与CxFreeze也输出库的方式不同。

For Python 3.2 scripts the only choice is Cxfreeze. Build it from sources otherwise it won’t work.

For python 2.x I suggest pyinstaller as it can package a python program in a single executable, unlike CxFreeze which outputs also libraries.


回答 11

py2exe将生成您想要的exe文件,但您需要在要使用新exe的计算机上具有相同版本的MSVCR90.dll。有关更多信息,请参见http://www.py2exe.org/index.cgi/Tutorial

py2exe will make the exe file you want but you need to have the same version of MSVCR90.dll on the machine you’re going to use your new exe. See http://www.py2exe.org/index.cgi/Tutorial for more info.


回答 12

你可以找到的上市@配电公司名单https://wiki.python.org/moin/DistributionUtilities

我使用bbfreeze,并且运行良好(虽然还支持python 3)。

You can find the list of distribution utilities listed @ https://wiki.python.org/moin/DistributionUtilities.

I use bbfreeze and it has been working very well (yet to have python 3 support though).


回答 13

pyinstaller yourfile.py -F --onefile

这将在Windows中创建一个独立的EXE文件。重要说明:该exe文件将在名为“ dist”的文件夹中生成。

您可以使用安装pyinstaller pip install PyInstaller

pyinstaller yourfile.py -F --onefile

This creates a standalone EXE file in Windows IMPORTANT NOTE: The exe file will be generated in a folder named ‘dist’

you can install pyinstaller using pip install PyInstaller


回答 14

不完全是python代码的打包,但是google 现在也有脾气暴躁的人,它将代码转换为Go。它不支持python C api,因此它可能不适用于所有项目。

Not exactly a packaging of the python code, but there is now also grumpy from google, which transpiles the code to Go. It doesn’t support the python C api, so it may not work for all projects.


回答 15

使用pyinstaller,我发现了使用.exe快捷方式而不是make的更好方法--onefile。无论如何,周围可能都有一些数据文件,如果您正在运行基于站点的应用程序,则您的程序也将依赖于html,js,css文件。将所有这些文件移到某个地方没有意义..相反,如果我们将工作路径向上移怎么办。

为exe创建快捷方式,将其移动到顶部,并按指定设置目标路径和开始路径,以使相对路径进入dist \ folder: Target: %windir%\system32\cmd.exe /c start dist\web_wrapper\web_wrapper.exe Start in: "%windir%\system32\cmd.exe /c start dist\web_wrapper\" 可以将快捷方式重命名为任何名称,从而重命名为“ GTFS-Manager”
现在当我双击-单击快捷方式,就好像我是python运行文件一样!我发现这种方法比以下方法更好--onefile

  1. 在onefile的情况下,存在Win7 OS缺少.dll的问题,该文件需要事先安装等。使用具有多个文件的常规构建,不会出现此类问题。
  2. 我的python脚本使用的所有文件(正在部署龙卷风Web服务器,并且需要整个freakin’网站上有价值的文件!)不需要移动到任何地方:我只是在顶部创建了快捷方式。
  3. 我实际上可以在ubuntu(运行python3 myfile.py)和Windows(双击快捷方式)中使用完全相同的文件夹。
  4. 我不需要为.spec文件的过于复杂的黑客行为而烦恼,以包括数据文件等。

哦,切记在构建后删除构建文件夹,这样可以节省大小。

Using pyinstaller, I found a better method using shortcut to the .exe rather than making --onefile. Anyways there’s probably some data files around and if you’re running a site-based app then your program depends on html, js, css files too. No point in moving all these files somewhere.. instead what if we move the working path up.

Make a shortcut to the exe, move it at top and set the target and start-in paths as specified, to have relative paths going to dist\folder: Target: %windir%\system32\cmd.exe /c start dist\web_wrapper\web_wrapper.exe Start in: "%windir%\system32\cmd.exe /c start dist\web_wrapper\" Can rename shortcut to anything so renaming to “GTFS-Manager”
Now when I double-click the shortcut, it’s as if I python-ran the file! I found this approach better than the --onefile one as:

  1. In onefile’s case, there’s a problem with a .dll missing for win7 OS which needs some prior installation etc. Yawn. With the usual build with multiple files, no such issues.
  2. All the files that my python script uses (it’s deploying a tornado web server and needs a whole freakin’ website worth of files to be there!) don’t need to be moved anywhere: I simply create the shortcut at top.
  3. I can actually use this exact same folder in ubuntu (run python3 myfile.py) and windows (double-click the shortcut).
  4. I don’t need to bother with the overly complicated hacking of .spec file to include data files etc.

Oh, remember to delete off the build folder after building, will save on size.


回答 16

使用Cython转换为c,编译并与gcc链接。另一个可能是,在c中创建核心功能(要使其难以逆转的功能),对其进行编译,然后使用python boost导入已编译的代码(此外,您还将获得更快的代码执行速度)。然后使用提到的任何工具进行分发。

Use Cython to convert to c, compile and link with gcc. Another could be, make the core functions in c (the ones you want to make hard to reverse), compile them and use python boost to import the compiled code ( plus you get a much faster code execution). then use any tool mentioned to distribute.


回答 17

有人告诉我PyRun,https://www.egenix.com/products/python/PyRun/ 也是一种选择。