标签归档:executable

如何将python脚本编译为二进制可执行文件

问题:如何将python脚本编译为二进制可执行文件

我需要将Python脚本转换为Windows可执行文件。

我已将Python 2.6安装到python26

我创建了一个脚本并将其保存在中C:\pythonscript。在此文件夹中有两个文件

Setup.pyoldlogs.py(此文件需要掩盖)

setup.py 代码是

from distutils.core import setup
import py2exe

setup(console=['oldlogs.py'])

如何将oldlogs.py转换为exe文件?

I need to convert a Python script to a Windows executable.

I have Python 2.6 installed to python26.

I have created one script and kept it in C:\pythonscript. Inside this folder there are two files

Setup.py and oldlogs.py (this file need coversion)

setup.py code is

from distutils.core import setup
import py2exe

setup(console=['oldlogs.py'])

How can I convert oldlogs.py to an exe file?


回答 0

或者使用PyInstaller替代py2exe。这是一个很好的起点。PyInstaller还可以让您为Linux和Mac创建可执行文件。

这是一个相当容易使用PyInstaller解决当前问题的方法:

pyinstaller oldlogs.py

从该工具的文档中:

PyInstaller分析myscript.py并执行以下操作:

  • 在脚本所在的文件夹中写入myscript.spec。
  • 如果脚本不存在,则在与脚本相同的文件夹中创建文件夹。
  • 在构建文件夹中写入一些日志文件和工作文件。
  • 如果脚本不存在,则在与脚本相同的文件夹中创建文件夹dist。
  • 将myscript可执行文件文件夹写入dist文件夹。

在dist文件夹中,您找到分发给用户的捆绑应用程序。

Or use PyInstaller as an alternative to py2exe. Here is a good starting point. PyInstaller also lets you create executables for linux and mac…

Here is how one could fairly easily use PyInstaller to solve the issue at hand:

pyinstaller oldlogs.py

From the tool’s documentation:

PyInstaller analyzes myscript.py and:

  • Writes myscript.spec in the same folder as the script.
  • Creates a folder build in the same folder as the script if it does not exist.
  • Writes some log files and working files in the build folder.
  • Creates a folder dist in the same folder as the script if it does not exist.
  • Writes the myscript executable folder in the dist folder.

In the dist folder you find the bundled app you distribute to your users.


回答 1

我建议使用PyInstaller,可以使用以下命令将简单的python脚本转换为exe:

utils/Makespec.py [--onefile] oldlogs.py

这将创建一个yourprogram.spec文件,该文件是用于构建最终exe的配置。下一条命令从配置文件生成exe:

utils/Build.py oldlogs.spec

这里可以找到更多

I recommend PyInstaller, a simple python script can be converted to an exe with the following commands:

utils/Makespec.py [--onefile] oldlogs.py

which creates a yourprogram.spec file which is a configuration for building the final exe. Next command builds the exe from the configuration file:

utils/Build.py oldlogs.spec

More can be found here


回答 2

# -*- mode: python -*-

block_cipher = None

a = Analysis(['SCRIPT.py'],
             pathex=[
                 'folder path',
                 'C:\\Windows\\WinSxS\\x86_microsoft-windows-m..namespace-downlevel_31bf3856ad364e35_10.0.17134.1_none_50c6cb8431e7428f',
                 'C:\\Windows\\WinSxS\\x86_microsoft-windows-m..namespace-downlevel_31bf3856ad364e35_10.0.17134.1_none_c4f50889467f081d'
             ],
             binaries=[(''C:\\Users\\chromedriver.exe'')],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='NAME OF YOUR EXE',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )
# -*- mode: python -*-

block_cipher = None

a = Analysis(['SCRIPT.py'],
             pathex=[
                 'folder path',
                 'C:\\Windows\\WinSxS\\x86_microsoft-windows-m..namespace-downlevel_31bf3856ad364e35_10.0.17134.1_none_50c6cb8431e7428f',
                 'C:\\Windows\\WinSxS\\x86_microsoft-windows-m..namespace-downlevel_31bf3856ad364e35_10.0.17134.1_none_c4f50889467f081d'
             ],
             binaries=[(''C:\\Users\\chromedriver.exe'')],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='NAME OF YOUR EXE',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

如何使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/ 也是一种选择。