PyInstaller 2.0 捆绑文件为 --onefile [英] PyInstaller 2.0 bundle file as --onefile

查看:65
本文介绍了PyInstaller 2.0 捆绑文件为 --onefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PyInstaller 2.0 将我的 py 脚本捆绑为 .exe.我可以捆绑脚本,但是在我的脚本中,我需要打开一个应该捆绑在 exe 中的文件(因此它是可移植的).我在执行此操作时遇到问题..

在我的 .py 中,我有

filename = 'C:\path\to\my\file\doc.docx'数据 = 打开(文件名,'rb')

我使用 PyInstaller 2.0,这在我的计算机上运行良好,但是如果我将 exe 传输到另一台计算机,它将无法运行.. PyInstaller 2.0 是相当新的,因此其上的文档很少,并且出版商的文档相当缺乏".

以下是发布者关于此事的信息:(我认为他们的文档不是最新的,因为一开始它说使用 Configure.py,然后在其他文档中它说不再需要 Configure.py2.0)

<块引用>

在 --onefile 发行版中,数据文件捆绑在可执行文件中,然后在运行时通过 C 代码(也能够重建目录树)提取到工作目录中.工作目录最好通过 os.environ['_MEIPASS2'] 找到.因此,您可以通过以下方式访问这些文件:

os.path.join(os.environ["_MEIPASS2"], relativename))

这对我这个初学者来说真的没有意义..

来自出版商的另一份文件说..

<块引用>

在 --onefile 发行版中,数据文件捆绑在可执行文件中,然后在运行时提取通过 C 代码进入工作目录(它也能够重建目录树).工作目录最好通过 sys._MEIPASS 找到.因此,您可以通过以下方式访问这些文件:

os.path.join(sys._MEIPASS, relativename))

我对 os.environ["_MEIPASS2"] 进行了大量实验,但 python 似乎不理解 os.environment["_MEIPASS2"].我拿回来了:

<预><代码>>>>打印 os.environ["_MEIPASS2"]回溯(最近一次调用最后一次):文件<pyshell#0>",第 1 行,在 <module> 中打印 os.environ["_MEIPASS2"]文件C:\Python27\lib\os.py",第 423 行,在 __getitem__ 中返回 self.data[key.upper()]密钥错误:'_MEIPASS2'

我也尝试过 sys._MEIPASS .. 是的,python 响应模块"没有属性_MEIPASS".

在这一点上,我觉得我的头快要爆炸了..我感谢 PyInstaller 的作者的工作,但他们的文档是我见过的最糟糕的文档!我只需要有人帮我将文件捆绑到 exe 中.我真的很想使用 PyInstaller 2.0+,因为所有 .spec 的东西都让我与之前版本的 PyInstaller 混淆.

顺便说一句,我使用的是带有 python 2.7.3 的 Win8 64 位

请帮助!

解决方案

OMG!这个 PyInstaller 真的让我有点困惑.如果我之前的帖子听起来有点愤怒",那么抱歉......无论如何,对于任何试图在 --onefile PyInstaller 包中包含文件的人来说,这对我有用:

将其包含在您的 .py 脚本中:

filename = 'myfilesname.type'如果 hasattr(sys, '_MEIPASS'):# PyInstaller >= 1.6chdir(sys._MEIPASS)文件名 = 加入(sys._MEIPASS,文件名)elif '_MEIPASS2' 在环境中:# PyInstaller <1.6(仅在 1.5 上测试)chdir(环境['_MEIPASS2'])文件名=加入(环境['_MEIPASS2'],文件名)别的:chdir(dirname(sys.argv[0]))文件名 = 加入(目录名(sys.argv[0]),文件名)

感谢网上某个我不记得名字的人..(抱歉来晚了,我已经筋疲力尽了!)

然后,如果您使用的是 PyInstaller2.0,在 cmd 中,从 pyinstaller-2.0 目录,您可以运行

pyinstaller.py --onefile myscriptsname.py

这将在 pyinstaller-2.0 目录中创建一个 myscriptsname.spec 文件.它还会创建一个 exe,但这不起作用.稍后会更新.现在编辑该 .spec,并添加以下 a.datas 行(记住数据,而不是数据).我在 .spec 文件中添加了一些额外内容,仅供参考.

a = Analysis(['ServerTimeTest_nograph.py'],pathex=['c:\\Python27\\pyinstaller-2.0'],隐藏进口=[],钩子路径=无)a.datas += [('myfilesname.type','C:\\path\\to\\my\\file\\myfilesname.type','DATA')]pyz = PYZ(a.pure)

现在,回到cmd,运行

pyinstaller.py --onefile myscriptsname.spec

这将更新/dist 目录中的 .exe.

也许有更好的方法,或者更漂亮的方法,但这对我有用!

I'm trying to bundle my py script as an .exe using PyInstaller 2.0. I am able to bundle the script, but in my script, I need to open a file that should be bundled in the exe (so it's portable). I'm having trouble doing this..

In my .py, I have

filename = 'C:\path\to\my\file\doc.docx'
data = open(filename,'rb')

I use PyInstaller 2.0 and this works fine on my computer, but if I transfer the exe to another computer it isn't going to work.. PyInstaller 2.0 is pretty new, so there are very few documents on it, and the publisher's documentation is quite "lacking."

Here is the publisher's info on the matter: (I don't think their documentation is up to date, because in the beginning it says use Configure.py, then in other docs it says Configure.py is no longer needed in 2.0)

In a --onefile distribution, data files are bundled within the executable and then extracted at runtime into the work directory by the C code (which is also able to reconstruct directory trees). The work directory is best found by os.environ['_MEIPASS2']. So, you can access those files through:

os.path.join(os.environ["_MEIPASS2"], relativename))

That doesn't really make sense to me, a beginning programmer..

A different document from the publisher says..

In a --onefile distribution, data files are bundled within the executable and then extracted at runtime into the work directory by the C code (which is also able to reconstruct directory trees). The work directory is best found by sys._MEIPASS. So, you can access those files through:

os.path.join(sys._MEIPASS, relativename))

I've experimented around quite a bit with os.environ["_MEIPASS2"] and python doesn't seem to understand os.environment["_MEIPASS2"]. I get this back:

>>> print os.environ["_MEIPASS2"]

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    print os.environ["_MEIPASS2"]
  File "C:\Python27\lib\os.py", line 423, in __getitem__
    return self.data[key.upper()]
KeyError: '_MEIPASS2'

I also experimented with sys._MEIPASS.. Yeah, python responds 'module' has no attribute '_MEIPASS'.

At this point, I feel like my head is about to explode.. I appreciate PyInstaller's authors for their work, but their documentation is the worst I've ever seen! I just need someone to help me bundle my file into the exe. I would really like to use PyInstaller 2.0+ since all the .spec stuff confuses me with previous versions of PyInstaller.

BTW, I'm using Win8 64bit with python 2.7.3

PLEASE HELP!

解决方案

OMG! This PyInstaller really confused me for a bit. If my previous post sounds a little "ranty", sorry about that.. Anyways, for anyone trying to include a file in a --onefile PyInstaller package this worked for me:

Include this in your .py script:

filename = 'myfilesname.type'
if hasattr(sys, '_MEIPASS'):
    # PyInstaller >= 1.6
    chdir(sys._MEIPASS)
    filename = join(sys._MEIPASS, filename)
elif '_MEIPASS2' in environ:
    # PyInstaller < 1.6 (tested on 1.5 only)
    chdir(environ['_MEIPASS2'])
    filename = join(environ['_MEIPASS2'], filename)
else:
    chdir(dirname(sys.argv[0]))
    filename = join(dirname(sys.argv[0]), filename)

credit to someone online whose name I don't remember.. (sorry it's late and I'm exhausted!)

Then, if you're using PyInstaller2.0, in cmd, from the pyinstaller-2.0 dir, you can run

pyinstaller.py --onefile myscriptsname.py

That will create a myscriptsname.spec file in the pyinstaller-2.0 dir. It will also create an exe, but that won't work. It will be updated later. Now edit that .spec, and add the following a.datas line (remember datas, not data). I included a little extra in the .spec file just for reference.

a = Analysis(['ServerTimeTest_nograph.py'],
             pathex=['c:\\Python27\\pyinstaller-2.0'],
             hiddenimports=[],
             hookspath=None)
a.datas += [('myfilesname.type','C:\\path\\to\\my\\file\\myfilesname.type','DATA')]
pyz = PYZ(a.pure)

Now, back in cmd, run

pyinstaller.py --onefile myscriptsname.spec

This will update your .exe in the /dist dir.

Maybe there's a better way, or a prettier way, but this worked for me!

这篇关于PyInstaller 2.0 捆绑文件为 --onefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆