您的位置:首页 > 编程语言 > Python开发

win 下用 pyinstaller 转换python代码为可执行程序

2014-02-08 15:40 369 查看
选择 pyinstaller 而非 py2exe 的原因可参考: 关于python打包成exe的一点经验之谈 .

此时 pyinstaller 的最新版本为 2.1, 用 Google 搜到的相关文章基本为 2.0, 安装并不相同.
官网在 win 下的安装介绍为:


Installing in Windows

For Windows, PyWin32 is a prerequisite. Follow that link and carefully
read the instructions; there is a different version of PyWin32 for each version of Python. With this done you can continue to install pip using the MS-DOS command line.
However it is particularly easy to use pip-Win, which sets
up both pip and virtualenv and makes it simple to
install packages and to switch between different Python interpreters. (For more on the uses of virtualenv, see Supporting
Multiple Platforms below.)
When pip-Win is working, enter this command in its Command field and click Run:
venv -c -i pyi-env-name
This creates a new virtual environment rooted at C:\Python\pyi-env-name and makes it the current environment.
A new command shell window opens in which you can run commands within this environment. Enter the command
pip install PyInstaller
Whenever you want to use PyInstaller,

Start pip-Win
In the Command field enter venv pyi-env-name
Click Run

Then you have a command shell window in which commands execute in that environment.
可参考: http://pythonhosted.org/PyInstaller/

1. 安装 python2.7 .

2. 安装 PyWin32 .
3. 下载 pip-Win_1.6.exe

运行 pip-Win 会安装 pip 和 virtualenv, 在其中碰到两个问题.
> "C:\Python27\lib\mimetypes.py" 报 "UnicodeDecodeError"
这是一个 bug, 有个 patch 修复 http://bugs.python.org/file18143/9291.patch .
即将 mimetypes.py 中 "UnicodeEncodeError" 替换为 "UnicodeError" 即可.
> 下载 virtualenv 失败
手动在 https://pypi.python.org/pypi/virtualenv#downloads 下载 virtualenv-1.11.2-py2.py3-none-any.whl
执行 "C:\Python27\Scripts\pip.exe install virtualenv-1.11.2-py2.py3-none-any.whl" 即可.
4. 在 pip-Win 的 "Command" 输入框中输入 "venv -c -i pyis", 并执行. 以后要进入创建的 pyis 只需执行 "venv pyis" .






5. 新弹出的 cmd 窗口为创建的 pyis 环境, 在其中执行 "pip install PyInstaller" . 至此环境部署完成.
6. 在 pyis 环境中执行 "pyinstaller -F test.py", 将 python 源码转为一个独立的 exe 程序.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: