简体   繁体   English

py2exe - 从文件夹中创建单个可执行文件

[英]py2exe - Create single executable file out of a folder

Hello Stackoverflowers, 你好Stackoverflowers,

I have been trying to create a single executable file out of a folder which contains a python script and some other modules and files (note: I am also using Tkinter if that is important). 我一直在尝试从包含python脚本和其他一些模块和文件的文件夹中创建一个可执行文件(注意:如果这很重要,我也使用Tkinter)。

I have already looked at a lot of questions/answers relating to this and tried them but none of them seem to work for me. 我已经看过很多与此有关的问题/答案并尝试过,但它们似乎都不适合我。

Here is what my folder looks like: 这是我的文件夹的样子:

python-calendar                #base-folder
|
|___apliclient                 #module
|___httplib2                   #module
|___oauth2client               #module
|___uritemplate                #module
|___client_secrets.json        #used by program.py
|___program.py                 #my main script
|___program.dat                #updated by program.py

My question is: 我的问题是:
How can I create a single executable file that groups all these files/folders together into a single, standalone executable file that can be run? 如何创建单个可执行文件,将所有这些文件/文件夹组合在一个可以运行的单个独立可执行文件中?

This is what my setup.py file looks like for the py2exe installer at the moment however when I run my executable, nothing happens (both from the folder and from command-line). 这就是我的setup.py文件目前对于py2exe安装程序的样子,但是当我运行我的可执行文件时,没有任何反应(来自文件夹和命令行)。

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True, "includes" : ['apiclient','httplib2','oauth2client',
                        'uritemplate'] }},
    console = ["program.py"],
    zipfile = None,
    data_files=['client_secrets.json']
)

Any guidance on how to use py2exe for this or any other executable creator would be really helpful. 有关如何为此或任何其他可执行创建者使用py2exe任何指导都非常有用。 Thank you in advance. 先感谢您。

I found the solution in part. 我找到了部分解决方案。 First, modify your setup.py like this: 首先,修改你的setup.py,如下所示:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')
PROGRAM_DAT     = open('program.dat').read()
CLIENT_SECRETS  = open('client_secrets.json').read()

setup(windows=[{'script': "program.py",
            'other_resources': [
                 (u'PROGRAM_DAT', 1, PROGRAM_DAT), 
                 (u'CLIENT_SECRETS', 2, CLIENT_SECRETS) 
            ]
           }],
            options = {'py2exe': {'bundle_files': 1, 'compressed': True, "includes" : ['apiclient','httplib2','oauth2client',
                    'uritemplate']}
           },

           zipfile = None
 )

If you want to build console application, simply change setup(windows= on setup(console= . 如果你想构建控制台应用程序,只需更改setup(windows= on setup(console=

In program.py you can load resources like this: 在program.py中,您可以加载如下资源:

import win32api
from StringIO import StringIO

datfile = StringIO( win32api.LoadResource(0, u'PROGRAM_DAT', 1))
print datfile.getvalue()

secrets = StringIO( win32api.LoadResource(0, u'CLIENT_SECRETS', 2))
print secrets.getvalue()

But there is no way to modify program.exe from program.exe. 但是没有办法从program.exe修改program.exe。 To save your changes in embedded program.dat, you will need another exe-file. 要在嵌入式program.dat中保存更改,您将需要另一个exe文件。 Then, you can use win32api.BeginUpdateResource , win32api.UpdateResource and win32api.LoadResource functions. 然后,您可以使用win32api.BeginUpdateResourcewin32api.UpdateResourcewin32api.LoadResource函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM