我可以以某种方式“编译”一个 python 脚本以在没有安装 Python 的情况下在 PC 上工作吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4158369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I somehow "compile" a python script to work on PC without Python installed?
提问by Richard Knop
So I have a Python script:
所以我有一个 Python 脚本:
myscript.py
I am executing it like this:
我是这样执行的:
python D:\myscript.py
However, I must have Python installed and included in the PATHenvironment variable for that to work.
但是,我必须安装 Python 并将其包含在PATH环境变量中才能正常工作。
Is it somehow possible to "bundle" Python executable with a Python script so other people will be able to run it on their PCs without Python?
是否有可能以某种方式将 Python 可执行文件与 Python 脚本“捆绑”在一起,以便其他人无需 Python 就可以在他们的 PC 上运行它?
It is ok if it will work only in Windows.
如果它只能在 Windows 中工作就可以了。
EDIT:
编辑:
After trying the compile.pyI get this error:
尝试后,compile.py我收到此错误:
Traceback (most recent call last):
File "D:\stuff\compile.py", line 4, in <module>
import py2exe
ImportError: No module named py2exe
采纳答案by Corey Goldberg
Here is one way to do it (for Windows, using py2exe).
这是一种方法(对于 Windows,使用py2exe)。
First, install the py2exeon your Windows box.
首先,py2exe在您的 Windows 机器上安装。
Then create a python script named compile.py, like this:
然后创建一个名为 的python 脚本compile.py,如下所示:
import sys
from distutils.core import setup
import py2exe
entry_point = sys.argv[1]
sys.argv.pop()
sys.argv.append('py2exe')
sys.argv.append('-q')
opts = {
'py2exe': {
'compressed': 1,
'optimize': 2,
'bundle_files': 1
}
}
setup(console=[entry_point], options=opts, zipfile=None)
To compile your Python script into a Windows executable, run this script with your program as its argument:
要将 Python 脚本编译为 Windows 可执行文件,请使用您的程序作为参数运行此脚本:
$ python compile.py myscript.py
It will spit out a binary executable (EXE) with a Python interpreter compiled inside. You can then just distribute this executable file.
它将吐出一个二进制可执行文件 (EXE),其中编译有 Python 解释器。然后您就可以分发这个可执行文件。
回答by Michael
回答by Binary Phile
PyInstallerhas worked well for me, generating reasonably small packages due to its use of upx. Its dependency detection was better than py2exe at the time as well. It seems not to have a lot of recent development and probably doesn't work with 3.x, however.
PyInstaller对我来说效果很好,由于它使用了 upx,生成了相当小的包。它的依赖检测在当时也比 py2exe 好。它似乎没有很多最近的开发,但是可能不适用于 3.x。
The source in the repository is a better starting point than the 1.4 package.
存储库中的源代码是比 1.4 包更好的起点。
Also see the wiki page about working with Python 2.6+.
另请参阅有关使用 Python 2.6+ 的 wiki 页面。
From the features list:
从功能列表:
- Packaging of Python programs into standard executables, that work on computers without Python installed.
- Multiplatform: works under Windows (32-bit and 64-bit), Linux (32-bit and 64-bit) and Mac OS X (32-bit only for now, see MacOsCompatibility).
- Multiversion: works under any version of Python from 1.5 up to 2.7. NOTE: If you're using Python 2.6+ on Windows, see Python26Win.
- Flexible packaging mode:
- Single directory: build a directory containing an executable plus all the external binary modules (.dll, .pyd, .so) used by the program.
- Single file: build a single executable file, totally self-contained, which runs without any external dependency.
- Custom: you can automate PyInstaller to do whatever packaging mode you want through a simple script file in Python.
- Explicit intelligent support for many 3rd-packages (for hidden imports, external data files, etc.), to make them work with PyInstaller out-of-the-box (see SupportedPackages).
- Full single-file EGG support: required .egg files are automatically inspected for dependencies and bundled, and all the egg-specific features are supported at runtime as well (entry points, etc.).
- Partial directory EGG support: required .egg directories are automatically inspected for dependencies and bundled, but egg-specific features will not work at runtime.
- Automatic support for binary libraries used through ctypes (see CtypesDependencySupport for details).
- Support for automatic binary packing through the well-known UPX compressor.
- Optional console mode (see standard output and standard error at runtime).
- Windows-specific features:
- Support for code-signing executables.
- Full automatic support for CRTs: no need to manually distribute MSVCR*.DLL, redist installers, manifests, or anything else; true one-file applications that work everywhere!
- Selectable executable icon.
- Fully configurable version resource section and manifests in executable.
- Support for building COM servers.
- Mac-specific features:
- Preliminar support for bundles (see MacOsCompatibility)
- 将 Python 程序打包成标准可执行文件,可在未安装 Python 的计算机上运行。
- 多平台:适用于 Windows(32 位和 64 位)、Linux(32 位和 64 位)和 Mac OS X(目前仅 32 位,请参阅 MacOsCompatibility)。
- Multiversion:适用于从 1.5 到 2.7 的任何 Python 版本。注意:如果您在 Windows 上使用 Python 2.6+,请参阅 Python26Win。
- 软包装方式:
- 单个目录:构建一个包含可执行文件以及程序使用的所有外部二进制模块(.dll、.pyd、.so)的目录。
- 单个文件:构建一个单独的可执行文件,完全自包含,运行时没有任何外部依赖。
- 自定义:您可以通过 Python 中的简单脚本文件自动执行 PyInstaller 以执行您想要的任何打包模式。
- 对许多第 3 包(用于隐藏导入、外部数据文件等)的显式智能支持,使它们与 PyInstaller 开箱即用(请参阅 SupportedPackages)。
- 完整的单文件 EGG 支持:自动检查所需的 .egg 文件的依赖项并进行捆绑,并且在运行时也支持所有特定于 Egg 的功能(入口点等)。
- 部分目录 EGG 支持:自动检查所需的 .egg 目录的依赖项并进行捆绑,但特定于 Egg 的功能在运行时不起作用。
- 自动支持通过 ctypes 使用的二进制库(有关详细信息,请参阅 CtypesDependencySupport)。
- 通过著名的 UPX 压缩器支持自动二进制打包。
- 可选的控制台模式(参见运行时的标准输出和标准错误)。
- Windows 特有的功能:
- 支持代码签名可执行文件。
- 对 CRT 的全自动支持:无需手动分发 MSVCR*.DLL、重新分发安装程序、清单或其他任何内容;真正适用于任何地方的单一文件应用程序!
- 可选择的可执行图标。
- 完全可配置的版本资源部分和可执行文件中的清单。
- 支持构建 COM 服务器。
- Mac 特有的功能:
- 对捆绑包的初步支持(请参阅 MacOsCompatibility)
回答by user8583529
Go to Notepad++ Menu, Plugins --> Python Script --> Show Console. In newly opened console window, at bottom type..... execfile('C:/path/to/your/python/file/code.py') #Use forward slash (/) instead of backwared slash. Hit return or click on run button.
转到 Notepad++ 菜单,插件 --> Python 脚本 --> 显示控制台。在新打开的控制台窗口中,在底部键入..... execfile('C:/path/to/your/python/file/code.py') #使用正斜杠 (/) 而不是反斜杠。点击返回或点击运行按钮。

