pyinstaller相关错误
http://blog.csdn.net/pipisorry/article/details/50620495
目录结构
将主文件testMain.py转换成exe可执行文件
主文件调用了自定义包:nlptest,其中nlptest1.py读取文件stopwordsFile.txt。
错误1:[Errno 2] No such file or directory: 'C:\Users\...\AppData\Local\Temp\_MEI***'
问题:
这个问题实质上是python代码转换成exe时怎么添加数据文件[http://pythonhosted.org/PyInstaller/#adding-data-files]
python文件通过命令$pyinstaller -F E:/mine/python_workspace/NLP/TargetOpinion/TargetOpinionMain.py转换为exe文件时数据文件找不到[pyinstaller使用-python项目转换成exe可执行文件]
原来python文件中读取路径为stopwordsFile的文件with open(stopwordsFile, encoding='utf-8')as f
转换为exe后,只有一个exe文件了,exe文件执行时,会将资源释放到temp文件夹下再执行,但是原来的数据文件stopwordsFile并没打包和释放,这样就会找不到文件。
原因详解:
单独生成一个exe文件(-F参数)后,exe文件是怎么运行的:
How the One-File Program Works
The bootloader is the heart of the one-file bundle also. When started it creates a temporary folder in the appropriate temp-folder location for this OS. The folder is named _MEIxxxxxx, where xxxxxx is a random number.
The one executable file contains an embedded archive of all the Python modules used by your script, as well as compressed copies of any non-Python support files (e.g. .so files). The bootloader uncompresses the support files and writes copies into the the temporary
folder. This can take a little time. That is why a one-file app is a little slower to start than a one-folder app.
After creating the temporary folder, the bootloader proceeds exactly as for the one-folder bundle, in the context of the temporary folder. When the bundled code terminates, the bootloader deletes the temporary folder.
[http://pythonhosted.org/PyInstaller/#how-the-one-file-program-works]
解决方案:
方案1:
最后发现比较简单的解决方案只要将python代码中的数据文件拷贝到dist文件夹下,如dist/data/stopwordsFile.txt,在python代码中读取文件时文件路径为stopwordsFile=r"data/stopwordsFile.txt"就可以了。
为了python代码直接执行和exe执行都不会出错,可以这样写代码
添加一个判断,如果是pyinstaller生成的exe文件执行的,会在sys中(之前版本是这个os.environ?)添加一个_MEIPASS(即exe释放路径C:\Users\...\AppData\Local\Temp\_MEI***)和一个frozen [It
also adds the attributes frozen and _MEIPASS to
the sys built-in module],直接从python代码执行则不会
analysis.py
if getattr(sys, 'frozen', False):
pathname = r""
else:
]
print("pathname: " + pathname)
stopwordsFile = GlobalOptions.stopwordsFile
stopwordsFile = os.path.join(pathname, stopwordsFile)
class GlobalOptions():
'''
全局变量设置
'''
# 如果用pyinstaller转换后,对应于exe文件的dist目录,下面的文件要要放在exe同目录下
stopwordsFile = r'data/English_stopwords.txt'
要保证在dist/data目录下存在数据文件,并且python执行时analysis.py同目录下/data目录中也有数据文件,这样不管exe执行还是python代码执行都找得到数据文件。
[pyinstaller打包selenium找不到webdriver_prefs.json]
方案2:(目前推荐这个,有更好的方案lz会更新)
修改中间文件,再去生成exe文件,这样生成exe文件时将python项目中的数据文件加入到生成的exe bundle的文件中,exe文件执行时,会释放txt文件到temp目录下。
为了python代码直接执行和exe执行都不会出错,可以这样写代码:
添加一个判断,如果是pyinstaller生成的exe文件执行的,会在sys中(之前版本是这个os.environ?)添加一个_MEIPASS(即exe释放路径C:\Users\...\AppData\Local\Temp\_MEI***),数据文件也会在这个目录下。和一个frozen
[It also adds the attributes frozen and _MEIPASS to
the sys built-in module],直接从python代码执行则不会。
analysis.py:
if getattr(sys, 'frozen', False):
pathname = sys._MEIPASS
else:
]
# print("pathname: " + pathname)
patternFile = GlobalOptions.patternFile
patternFile = os.path.join(pathname, patternFile)
with open(stopwordsFile, encoding='utf-8')as f: ...
Note: lz要提醒的是:os.path.split(os.path.realpath(__file__))[0]这个路径在exe文件中实际就是C:\Users\...\AppData\Local\Temp\_MEI***\test0\nlptest也就是路径sys._MEIPASS\python项目顶层目录\...\analysis.py
[http://pythonhosted.org/PyInstaller/#using-file-and-sys-meipass]
[PyInstaller executable issue]
1. 同方案1在原来的python项目读取数据文件的pythony文件中添加一个判断,如果是pyinstaller生成的exe文件执行的,会在sys中添加一个_MEIPASS(即exe释放路径C:\Users\...\AppData\Local\Temp\_MEI***)和一个frozen
[It also adds the attributes frozen and _MEIPASS to
the sys built-in module],直接从python代码执行则不会
2. 修改spec文件中analysis.datas如下:
a = Analysis(...
datas= [ ('nlptest/data/stopwordsFile.txt', 'data/stopwordsFile.txt' ) ],
)
datas内部元组参数解释:
- The first string specifies the file or files as they are in this system now.
- The second specifies the name of the folder to contain the files at run-time.
或者在a = Analysis()下面加上代码,告诉pyinstaller打包python文件时也将数据文件加入进去:
这个数据结构是TOC结构,类似数组列表a TOC object contains a list of tuples of the form (name,path,typecode)
a.datas+= [('stopwordsFile.txt', r'nlptest\stopwordsFile.txt', 'DATA'),]
a.datas += [('data/patterns.txt', 'TargetOpinion/Algorithm/data/patterns.txt', 'DATA'),
('data/English_stopwords.txt', 'TargetOpinion/Algorithm/data/English_stopwords.txt', 'DATA'),]
Note:第一个参数代码exe文件释放时候数据文件在temp/_mei***目录下的目录位置(也就是python代码中读取文件时候的位置(python代码中(python项目顶层目录为e/mine/py/nlp/时)e/mine/py/nlp/target/a.txt在exe释放时就成了C:\Users\...\AppData\Local\Temp\_MEI***\nlp/target/a.txt)),第二个参数代表要读取的数据文件的真实目录(没打包前的目录,spec文件相对路径?或者绝对路径),最后一个代表文件类型为数据文件。
| typecode | description | name | path |
|---|---|---|---|
| 'DATA' | Arbitrary files. | Run-time name. | Full path name in build. |
| 'BINARY' | A shared library. | Run-time name. | Full path name in build. |
| 'EXTENSION' | A binary extension to Python. | Run-time name. | Full path name in build. |
| 'OPTION' | A Python run-time option. | Option code | ignored. |
[http://pythonhosted.org/PyInstaller/#the-toc-and-tree-classes]
当然也可以用tree数据结构读取目录下的所有文件,再将tree结构转换成toc结构:
a.datas += Tree('TargetOpinion/Algorithm/data/', prefix='data', excludes=[''], typecode='DATA')
这个等价于a.datas += [('data/patterns.txt', 'TargetOpinion/Algorithm/data/patterns.txt', 'DATA'), ('data/English_stopwords.txt', 'TargetOpinion/Algorithm/data/English_stopwords.txt', 'DATA'),]
Note: Tree(root, prefix=run-time-folder, excludes=string_list, typecode=code | 'DATA' )
参数root代表python代码中读取文件位置(没打包前的目录,spec文件相对路径?或者绝对路径),参数prefix代表运行时exe文件释放目录temp/_mei***下的相对子目录,参数excludes代表目录下要排除的文件如"*.pyc",typecode代表文件类型为数据文件。
[http://pythonhosted.org/PyInstaller/#the-tree-class]
3. 再通过spec文件生成单文件exe文件:
pyinstaller -F E:/mine/python_workspace/test0/testMain.spec
或者
from PyInstaller.__main__ import run
if __name__ == '__main__':
opts = ['TargetOpinionMain.spec', '-F']
run(opts)
[Bundling
data files with PyInstaller (--onefile)]
[http://pythonhosted.org/PyInstaller/#adding-data-files]
方案3:
使用python自带包pakutil
testMain.py
import nlptest
nlptest1.py
pkgutil.get_data('nlptest', patternFile)
但是这个和open打开是一样的,转换成exe后还是会找不到文件,并且如果不在testMain.py中添加import nlptest,get_data返回值为None。
另外要注意的问题
with open("***.txt", encoding='utf-8')as f
txt文件所在目录对应的是这个代码执行的目录(可能被另一个目录中的文件调用,则目录就是那个文件所在目录),而不是当前所在的文件a.py所在的目录。
错误2:pag_resources.distributionnotfound: pyinstaller testMain.py returned -1
在文件nlptest1.py中加入这一行出错
from PyInstaller.building.build_main import Analysis
就是引入PyInstaller出错,也不知道为什么,不加入就可以了,我的加入只是测试一个东西。
from:http://blog.csdn.net/pipisorry/article/details/50620495
ref:
pyinstaller相关错误的更多相关文章
- OpenStack虚机相关错误
OpenStack配置起来还是挺麻烦的,特别是网络那块.虽然官方文档越来越清晰,但有时还是会出各种错.排错主要是看日志.看官方文档和google 以下就一些虚机相关常见的错误做一下总结(基于Iceho ...
- 文件 "c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ttt.mdf" 已压缩,但未驻留在只读数据库或文件组中。必须将此文件解压缩。 CREATE DATABASE 失败。无法创建列出的某些文件名。请查看相关错误。 (.Net SqlClient Data Provider)
问题: 文件 "c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ttt.mdf" 已压缩,但 ...
- Linux 下Redis集群安装部署及使用详解(在线和离线两种安装+相关错误解决方案)
一.应用场景介绍 本文主要是介绍Redis集群在Linux环境下的安装讲解,其中主要包括在联网的Linux环境和脱机的Linux环境下是如何安装的.因为大多数时候,公司的生产环境是在内网环境下,无外网 ...
- thinkphp5.0的验证码安装和相关错误
thinkphp5.0的验证码安装和相关错误 问题 只要是之前使用thinkphp5框架搭建网站的时候发现不管如何调用验证码都无法使用,按照官网要求,使用composer安装验证码出现报错Fatal ...
- 21.pyinstaller相关参数
pyinstaller相关参数 命令 描述 -F, ...
- pyinstaller相关问题 & pygame文件打包成exe文件 & 武装飞船 & 飞机大战
自己照书写了一个飞机大战游戏的python程序,想把它打包成一个exe文件,在查阅相关教程并经过数次尝试后终于成功. 安装打包应用 pyinstaller 在cmd命令窗口下pip install p ...
- vc编译器 msvcr.dll、msvcp.dll的含义和相关错误的处理
转自:http://blog.csdn.net/sptoor/article/details/6203376 很久没有写程式设计入门知识的相关文章了,这篇文章要来谈谈程式库 (Library) 连结, ...
- 【XCode7+iOS9】http网路连接请求、MKPinAnnotationView自定义图片和BitCode相关错误--备用
更新了iOS9和XCode7,之后,Swift变成了2.0,有了新的语法习惯,iOS也加强了安全方面的限制.我们原本的项目就会出现不少问题.先来看我之前的项目中出现的3个错误吧和相关的解决办法吧. 1 ...
- Hibernate 关联查询 相关错误
错误提示: could not resolve property: 确定有相关属性时,记得使用 criteria.createAlias @ManyToOne 若可能为null 要加上 @NotFou ...
随机推荐
- Java锁机制了解一下
前言 回顾前面: 多线程三分钟就可以入个门了! Thread源码剖析 多线程基础必要知识点!看了学习多线程事半功倍 只有光头才能变强! 本文章主要讲的是Java多线程加锁机制,有两种: Synchro ...
- 解决nodejs中json序列化时Date类型默认为UTC格式
在nodejs中,json序列化时Date类型时,默认转为UTC格式. 如下图 上面只是一个例子,下面我用一个更具体化的例子来展示一个这个情况,我们在开发WEB项目中,经常用到Express组件, 我 ...
- 给div添加2个class
<div class='center nocontent'> 类名用空格分开 在优先级一样的情况下就近原则 优先级# 100 .10 <span>这种标签是1 所以的相加 ...
- Tomcat和JDK的内存配置
1.jvm内存管理机制: 1)堆(Heap)和非堆(Non-heap)内存 按照官方的说法:"Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Ja ...
- 装 ubuntu + win10 出现 grub rescue 并处理之
开机出现 grub rescue 原因:装 ubuntu + win10 双系统时有可能搞坏启动文件. grub rescue 隶属于 ubuntu管理. grub rescue 里可用命令很少,主要 ...
- jquery插件存档
1.选择插件selectMenu github地址:https://github.com/josiaho/selectMenu 2.选择插件bootstrap_multiselect 官方地址:htt ...
- vue项目开发中遇到的问题总结--内部分享
1.路由变化页面数据不刷新问题 这种情况一般出现在vue-router的history模式下,初次进入会执行钩子函数,再次进入时则不会. 解决方案: 监听路由变化 watch : { "$ ...
- linux tar解压命令
linux下使用tar命令 解压语法:tar [主选项+辅选项] 文件或者目录 使用该命令时,主选项是必须要有的,它告诉tar要做什么事情,辅选项是辅助使用的,可以选用.主选项:c 创建新的档案文件. ...
- Sybase identity 字段
1.identity Oracle, DB2, pgSQL中都有sequence的概念,这个概念比Identity先进很多,在Sybase中没有Sequence对象,与之相对应的是Identity 2 ...
- OpenResty 执行阶段的概念和用途
主要还是 Nginx 的执行阶段知识了,都是因为 OR 才会那么深刻, 它有些自己的阶段. 主要还是参照 春哥的 Nginx 教程 请多读几遍,如果不清楚nginx的执行阶段就无法充分利用 openr ...