Heim  >  Artikel  >  Backend-Entwicklung  >  使用PyInstaller将Python程序文件转换为可执行程序文件

使用PyInstaller将Python程序文件转换为可执行程序文件

WBOY
WBOYOriginal
2016-07-22 08:56:241591Durchsuche

Windows下采用PyInstall将py文件转换成exe可执行文件
好不容易写完的py文件,想做成exe文件,最开始选择用py2exe,结果生成的exe遇到两个问题,
1. py程序里print 的信息,cmd中执行tool后并没有显示在屏幕上;
2. 调用dll接口,执行py文件时是阻塞等待的,结果cmd中执行exe时,tool很快先返回了,接口还在运行,很诡异
一时找不到解决的办法,无奈弃用了,在网上找了pyinstall,试了一下,感觉功能更强大些。。。
安装PyInstaller之前需确认首先安装了pywin32
下载地址:http://nchc.dl.sourceforge.net/project/pywin32/pywin32/Build%20218/pywin32-218.win32-py2.7.exe
 
一、PyInstaller安装
1.下载地址:http://www.pyinstaller.org/wiki
2.最新版本:PyInstaller 2.0
3.直接“解压缩”之后即可使用,解压到您想让他在的路径即可
 
二、PyInstaller配置
1.事先写好py程序
2.在命令行执行:

Python Makespec.py --console --onefile NotePad\notepad.py

报错:

Configfile is missing or unreadable. Please run Configure.py before building

3.在命令行执行:

Configure.py

报错:

Python 2.6+ on Windows support needs pywin32,Please install http://sourceforge.net/projects/pywin32/

4.安装最新版本的 pywin32-217.win32-py2.7.exe,下载地址:http://sourceforge.net/projects/pywin32/files/pywin32/Build%20217/
 
三、PyInstaller使用
PyInstaller的使用相对简单,cmd下进入解压出来的目录,执行如下命令:
python pyinstaller.py [opts] yourprogram.py

四、主要选项包括:
-F, -onefile 打包成一个exe文件
-D, -onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)
-c, -console, -nowindowed 使用控制台,无界面(默认)
-w, -windowed, -noconsole 使用窗口,无控制台

五、注意事项
在转换之前要确保py程序可以正常通过;
如果转换过程中出现什么错误,用搜索引擎搜索解决一下。


Linux下安装pyinstaller用于将py文件打包生成一个可执行文件
一、安装使用流程

1. 首先给系统装个easy_install, 如果装了的可以跳过这步
到pypi官方网址 https://pypi.python.org/pypi/setuptools 去download最新版本

2. 官网上下载pyinstaller,当前的最新版本是2.1
(1) 网址 http://www.pyinstaller.org/

3.解包进入源码目录
(1)

tar -zxvf pyinstaller_2.1.tar.gz

(2)

cd pyinstaller-2.1

运行

python setup.py install

4. 拷贝py文件
将需打包的py文件如test.py 拷贝到当前目录

5. 生成可执行文件
cd到pyinstaller目录, 执行

python pyinstaller.py test.py

二、可能遇到的问题

1. 用户权限
直接在root用户下运行pyinstaller会报错 “cannot run pyinstaller as user root"...
所以需新增另外一个用户,并给用户对pyinstaller所在的目录及所有父目录添加读写权限,避免运行后报‘mkdir' permission denied...

2. 找不到python lib动态库
su切换到新增用户后,
pyinstaller报错找不到python动态库

raise IOError("Python library not found!")
IOError: Python library not 

执行python --version查看当前版本,并查找/usr/local/lib 下有没有对应版本的libpython*.*.so文件,
如果有则跳过这步,如果没有则需要重新安装python,在python安装配置中一定要加上--enable-shared参数就可以生成对应动态链接库,如:

#./configure --enable-shared --prefix=/usr/local
#make
#make install

我环境中更新过python版本,导致/usr/lib下不是最新版本的库文件,需要将新版本拷贝到对应的/usr/lib目录下,注意有两个文件:

#cp libpython*.so /usr/lib/
#ln -s /usr/lib/libpython*.so.1.0 /usr/lib/libpython*.so

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn