【问题标题】:cx_freeze not importing external modulescx_freeze 不导入外部模块
【发布时间】:2014-02-27 16:40:55
【问题描述】:

我选择尝试使用 cx_freeze,它将我的简单 python 3.x 键盘记录器转换为 exe。我选择 cx_freeze 因为 py2exe 只是 python 2.x 我正在使用这个 setup.py 脚本编译我的代码。

from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])

base = 'Console'

executables = [
    Executable('logger.py', base=base, targetName = 'logger.exe')
]

setup(name='PyLogger',
      version = '0.1',
      description = 'A Simple Keylogger',
      options = dict(build_exe = buildOptions),
      executables = executables)

当我编译我的代码时,我是

try:
    import pythoncom
except ImportError:
    input("Import Error, pywin32 is not installed")

try:
    import pyHook
except ImportError:
    input("Import Error, pyHook is not installed")

我收到导入错误,提示未安装 pywin32 和 pyHook。如何将外部模块导入 cx_freeze。

编辑 - 我尝试更改 setup.py 以添加包含选项,但没有任何区别。

from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = ['pyHook','pythoncom'],includes = ['pyHook','pythoncom'], excludes = [])

base = 'Console'

executables = [
    Executable('logger.py', base=base, targetName = 'logger.exe')
]

setup(name='PyLogger',
      version = '0.1',
      description = 'A Simple Keylogger',
      options = dict(build_exe = buildOptions),
      executables = executables)

【问题讨论】:

  • 你能显示冻结应用程序的输出吗?另外,这些包裹是用拉链鸡蛋包装的吗? cx_Freeze 目前无法在压缩鸡蛋中找到模块,但我有 a pull request 来解决这个问题。

标签: python pywin32 cx-freeze pyhook


【解决方案1】:

找到外部模块的 .pyd 文件。将其复制并粘贴到构建文件中。因此,例如,如果它正在寻找 _cpyHook(我遇到了与您相同的问题,并且它说模块丢失了),请转到 C:\Python33\Lib\site-packages\pyHook 并将文件复制并粘贴到 C :\Python33\build\exe.win-amd64-3.3.

【讨论】:

    【解决方案2】:

    尝试在构建选项中明确列出缺少的包,如下所示:

    buildOptions = dict(packages = ['pyHook', 'pywin32'], excludes = [])
    

    如果您需要在构建中包含其他(非 Python)文件,请参阅this question 的已接受答案。

    编辑:我终于有时间再看看这个,这似乎是一个棘手的问题。如果时间允许,我会继续研究它,但我想我会发布我的发现,以防它们对 OP 有用。我怀疑pyHook 模块在“冻结”时(即,当它包含在 zip 文件中时)播放效果不佳。如果我使用这个setup.py:

    from cx_Freeze import setup, Executable
    
    buildOptions = dict(
        includes=['pythoncom'],
        packages=['pyHook']
    )
    
    executables = [
        Executable('logger.py', base='Console', targetName = 'logger.exe')
    ]
    
    setup(
        name='PyLogger',
        version = '0.1',
        description = 'A Simple Keylogger',
        options = dict(build_exe = buildOptions),
        executables = executables
    )
    

    生成的logger.exe 至少在最初没有正确运行,并生成错误:

    Import Error, pyHook is not installed
    

    但是,如果我从包含 EXE 的目录中运行以下命令:

    unzip library.zip
    

    然后重新运行logger.exe,然后一切似乎都正常了。它只是无法从cx_Freeze 生成的library.zip 文件中加载pyHook。我以前曾见过这种问题,并通过在加载任何模块之前在我的顶级脚本中修改sys.path 来解决它。我会看看我是否可以挖掘其中一个例子。与此同时,也许这个建议会对 OP 有所帮助:尝试解压缩 zip 文件,看看它是否有所作为。需要注意的几点:

    • 导入pywin32 没有任何问题,只有pyHook
    • 确实尝试在构建选项中设置create_shared_zip=Falseinclude_in_shared_zip=False,但这只会导致文件名为logger.zip 而不是library.zip。 (奇怪。我不敢相信这不是错误。)

    【讨论】:

    • 抱歉,我认为您需要在 buildOptions 字典中设置 includes 而不是 packages。我目前无法验证这一点,但我会尽快检查并更新我的答案。
    • 刚刚更新了我的主要帖子,添加了包含,但是 pyHook 和 pywin32 仍未正确导入。
    猜你喜欢
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2010-11-25
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多