Skip to main content
 首页 » 编程设计

python之cx_freeze 不导入外部模块

2025年05月04日8xxx_UU

我选择尝试使用 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 以添加 includes 选项,但没有任何区别。

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) 

请您参考如下方法:

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