py2exe单个EXE文件中使用第三方库

Using third-party library in py2exe single EXE file

我正在 Python 2.7.9 中制作一些脚本文件,另外还使用了第三方库 pyautoit 0.4.

我想将我的脚本制作成单个 EXE(希望 运行 在单个文件中,这样它们就不需要设置文件)并使用 py2exe, 试过这个:

# -*- coding: utf-8 -*-
from setuptools import setup
import py2exe, sys, os
setup(name = "My Test Application",
      description = "My Test Application for Windows",
      version = "1.01",
      console = [{"script": "myprogram.py"}],
      #data_files - make directory at ./dist, not in exe file
      #data_files=[("./autoit/lib", ["AutoItX3.dll"])],
      options = {
          "py2exe": {
               "includes": ["win32api",,
                            "autoit",
                            "os" ,
                            "time"],
              "bundle_files": 1,
          }
    },
    zipfile = None,
)

已成功打包成单个EXE文件,但EXE中没有AutoItX3.dll文件

我发现 py2exe 无法在其 EXE 文件中包含 .dll 文件,因此在我的主脚本中我尝试手动导入 AutoItX3.dll:

# -*- coding: utf-8 -*-
import os, sys
from ctypes import cdll
autoitdll = cdll.LoadLibrary('./modules/AutoItX3.dll')
autoitdll.run("wordpad.exe")

而且无法加载。 (错误信息:

Traceback (most recent call last): File "C:/Users/win7x64kor/PycharmProjects/treesearch_27/myprogram.py", line 26, in autoitdll.run("wordpad.exe")

File "C:\Python27\lib\ctypes__init__.py", line 378, in getattr func = self.getitem(name)

File "C:\Python27\lib\ctypes__init__.py", line 383, in getitem func = self._FuncPtr((name_or_ordinal, self))

AttributeError: function 'run' not found

有什么方法可以加载包含.dll文件的库吗?

或者更简单的方法来包含 'pyautoit' 库功能,方法是添加一些选项,例如在 py2exe "options > includes : "autoit" " 单个 EXE 文件中?

如果您不需要将命令行参数传递给您的 exe,您可以尝试 http://www.py2exe.org/index.cgi/SingleFileExecutable

中建议的步骤

基本思想是使用 NSIS(Nullsoft 安装系统)将您的 exe 与所需的 dll 等捆绑在一起。当你的 exe 为 运行 时,它会将所有内容解压到临时文件夹中并执行。关闭时,临时文件会自动删除。