pyinstaller--将py文件转化成exe

首先要注意一下:打包python文件成exe格式这个过程只能在windows环境下运行

1. 直接在命令行用pip安装 pyinstaller

pip install pyinstaller</p>

2. 下载安装pyinstaler运行时所需要的windows拓展—pywin32

pywin32:http://sourceforge.net/projects/pywin32/files/pywin32/
点击最新的Build再点击pywin32-219.win-amd64-py2.7.exe(这里要根据你的windows x86或者x64和你的python版本来选择)


3.在命令行中切换到要打包的程序所在目录,直接输入下面的指令即可

pyinstaller -F demo.py
参数 含义 -F 指定打包后只生成一个exe格式的文件
-D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)
-c –console, –nowindowed 使用控制台,无界面(默认)
-w –windowed, –noconsole 使用窗口,无控制台
-p 添加搜索路径,让其找到对应的库。
-i 改变生成程序的icon图标

注意事项:

生成文件中,可能包含一个以warn开头的类似warndemo.txt的警告文件这很明显,是因为程序无法自动导入很多的库使用-p参数添加搜索路径

 pyinstaller.py -F -p C:\python27; ..\demo.py  #注意当前目录是我在下一级目录里

2.添加icon图标

pyinstaller.py -F -p C:\python27; -i ..\a.ico ..\demo.py

对了,这里有个网址可以参考一下
后话:
我按照自己写的步骤在自己电脑上试了一下是可以的,但是将exe拷贝到别人电脑上用了一下,发现出了一个这样的错误

F:\qq文件\a>AutoRecord.exe
Traceback (most recent call last):
  File "<string>", line 21, in <module>
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line
 507, in install
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line
 156, in __init__
ImportError: Can't load frozen modules.

这个问题是因为他的运行目录里面包含了中文,要处理中文要从github上面安装最新的pyinstaller

git clone https://github.com/dkw72n/pyinstaller.git
python setup.py install

然后重新打包.py文件再次发给对方就可以解决这个问题了
解决一些问题可以查看这个网址使用PyInstaller打包Python程序

运行的结果如下:

import urllib.request
import urllib.parse
import os
import json

content=input("请输入需要翻译的内容:")
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=http://www.youdao.com/'
data={}

data['type']='AUTO'
data['i']=content
data['doctype']='json'
data['xmlVersion']='1.8'
data['keyfrom:fanyi']='web'
data['ue']='UTF-8'
data['action']='FY_BY_CLICKBUTTON'
data['typoResult']='true'
data=urllib.parse.urlencode (data).encode('utf-8')

response = urllib.request.urlopen(url,data)
html=response.read().decode('utf-8')

target=json.loads(html)
print("翻译结果为:%s"%(target['translateResult'][0][0]['tgt']))
#input ('按任意键退出:')
os.system('pause')
#print(html)

这是一个利用python爬虫实现的翻译软件,利用有道词典的接口进行翻译,运行后,产生了一个translate.exe,可以在windows上未安装python的环境下运行。

完成

posted @ 2016-03-14 16:22  RicardoMJiang  阅读(293)  评论(0编辑  收藏  举报