如何设置PyInstaller使用Python 2版本进行编译?
让PyInstaller使用Python 2编译脚本的解决方法
以下几种方法可以解决你的问题,按优先级推荐:
1. 直接调用Python 2环境下的PyInstaller
如果已经给Python 2装了PyInstaller,直接用它的完整路径执行编译命令就行。比如你的Python 2安装在C:\Python27,命令应该是:
C:\Python27\Scripts\pyinstaller.exe your_old_script.py
这样就会强制使用Python 2的PyInstaller来处理脚本。
2. 用Python 2的pip重新安装PyInstaller
有时候系统里的PyInstaller可能绑定的是Python 3,你需要用Python 2的pip重新安装一遍,确保它属于Python 2环境:
python -m pip install pyinstaller==3.6
注意:PyInstaller 3.6是最后一批支持Python 2的版本,更高版本已不再兼容Python 2,所以必须指定这个版本号安装
安装完成后,直接运行pyinstaller your_old_script.py就会自动用Python 2编译。
3. 用虚拟环境隔离Python 2环境(推荐用于避免环境冲突)
如果你的电脑经常混用Python 2和3,用虚拟环境隔离最稳妥:
- 先给Python 2装virtualenv:
python -m pip install virtualenv - 创建一个Python 2的虚拟环境:
virtualenv py2_compile_env - 激活虚拟环境(PowerShell下):
. .\py2_compile_env\Scripts\Activate.ps1 - 激活后,安装适配Python 2的PyInstaller:
pip install pyinstaller==3.6 - 编译你的脚本:
pyinstaller your_old_script.py - 编译完成后,退出虚拟环境:
deactivate
内容的提问来源于stack exchange,提问作者TheTypicalDoge
相关产品推荐
相关产品推荐

