【问题标题】:how to bundle .py files launched with execfile() on py2exe?如何在 py2exe 上捆绑使用 execfile() 启动的 .py 文件?
【发布时间】:2013-05-10 14:15:38
【问题描述】:

我正在 Python 上开发一个小工具,它基本上在文件夹上启动一组脚本。我需要将它打包到一个独立的二进制文件中,并且我正在使用 py2exe。

我当前的代码使用 os.path.listdir() 获取文件夹中的所有 .py 文件,然后根据 PyQT 界面上的用户输入使用 execfile() 函数启动其中的一些文件。

如果通过主 Python 文件执行,我的代码会按预期工作,但在使用 py2exe 编译时会失败。例外是:

IOError: [Errno 2] No such file or directory

对于使用execfile() 启动的python 文件。

我目前正在与"bundle_files": 1zipfile = None 捆绑。我试图包含这些与包含和包混淆的文件,但没有运气。你能帮我正确配置py2exe吗?

这是我目前的setup.py

from distutils.core import setup
import py2exe
import os

#Python modules excluded from binary file
mod_excludes = [
    "Tkinter",
    "doctest",
    "unittest",
    "pydoc",
    "pygments",
    "pdb",
    "email",
    "_ssl",
    "difflib",
    "inspect"
]

#Avoid adding this dependencies
dll_excludes = [
    "MSVCP90.dll",
    "w9xpopen.exe"
]

#Force to exe
mod_includes = [
    "sip"
]

package_includes = [
    "app.payloads"
]



py2exe_options = {
    "optimize": 2,  # 0 (None), 1 (-O), 2 (-OO)
    "includes": mod_includes,
    "excludes": mod_excludes,
    "dll_excludes": dll_excludes,
    "packages": package_includes,
    #"xref": False,
    "bundle_files": 1,
    "compressed": True
    #"dist_dir": dist_dir
}

#TODO generar automaticamente la interfaz

setup(
    windows=[{"script": "app.py",
        "icon_resources": [(1, "app/gui/Res/app.ico")],
        "uac_info": "requireAdministrator"}],
    data_files=exe_files,
    options={"py2exe": py2exe_options},
    zipfile=None
    )

我得到以下回溯:

Traceback (most recent call last):
  File "app\gui\ui.pyo", line 22, in call_report
  File "app\core\core.pyo", line 32, in generate_report
  File "app\core\core.pyo", line 18, in launch_payload
IOError: [Errno 2] No such file or directory: 'C:\\Users\\my_user\\path\\to\\app\\dist\\app.exe\\app\\payloads\\autoruns.py'

【问题讨论】:

  • 您能否将您的setup.py 代码和尝试通过 python/py2exe 运行时的输出添加到问题中?
  • 谢谢巴斯!我已经添加了 setup.py 和 traceback。

标签: python windows py2exe


【解决方案1】:

Py2exe 仅包含 *.pyc 文件(或 .pyo 文件,如果您使用大于 0 的 "optimize" 像您一样)。由于您的错误消息提到了一个不存在的*.py 文件:

IOError: [Errno 2] 没有这样的文件或目录: 'C:\Users\my_user\path\to\app\dist\app.exe\app\payloads\autoruns.py'

,可能是这个原因。

一般来说,我建议不要使用execfile()。而是编写自己的包。如果您在应用程序代码的某处将其导入,Py2exe 将自动包含此包。这个包应该包含你想要动态加载的文件。您可以使用此代码:

my_module = __import__('my_package.module_name')

字符串'module_name'可以来自用户通过GUI输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2019-12-22
    • 1970-01-01
    相关资源
    最近更新 更多