cxfreeze打包python项目踩坑笔记

本文介绍使用cxfreeze将Python项目打包成Windows可执行文件的过程。遇到TypeError和OSError错误,通过cxfreeze-quickstart命令和在setup.py中添加include_files参数成功解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先说结论

  1. 推荐使用cxfreeze-quickstart命令来手动配置打包参数。
  2. 若项目包含有ctypes加载的dll文件,需要在setup.pybuildOptions内传入,include_files= ["xxx.dll"]

过程

目前需要打包一个python程序到一个没安装python环境的win平台上使用。查了查python打包exe的办法,对比后选择使用cxfreeze。

  1. 安装cxfreezepip install cx-freeze
  2. 开始打包,cmd运行cxfreeze --init-script=D:\py\test.py test.exe
> cxfreeze --init-script=D:\py\test.py test.exe
Traceback (most recent call last):
  File "d:\swdefault\python\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "d:\swdefault\python\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\swDefault\python\Scripts\cxfreeze.exe\__main__.py", line 7, in <module>
  File "d:\swdefault\python\lib\site-packages\cx_Freeze\main.py", line 174, in main
    silent = options.silent)
TypeError: __init__() missing 1 required positional argument: 'constantsModule'

运行时报错TypeError: \_\_init\_\_() missing 1 required positional argument: 'constantsModule'。解决方法 https://github.com/anthony-tuininga/cx_Freeze/issues/560

As a workaround, create a minimal setup.py manually or using the command line “cxfreeze-quickstart”.

看来cxfreeze命令暂时不能用,等以后更新吧。于是改用 cxfreeze-quickstart 命令。

  1. cmd运行cxfreeze-quickstart
> cxfreeze-quickstart
Project name: cxtest
Version [1.0]:
Description:
Python file to make executable from: D:\py\test.py
Executable file name [D:\py\test]: test.exe
(C)onsole application, (G)UI application, or (S)ervice [C]:
Save setup script to [setup.py]:
Overwrite setup.py [n]? y

Setup script written to setup.py; run it as:
    python setup.py build
Run this now [n]? y
running build
running build_exe
...省略一堆过程...
>

未报错,发现在当前文件夹下生成了build\exe.win-amd64-3.6\test.exe。那么就尝试运行这个可执行文件。

  1. cmd运行test.exe
> test.exe
Traceback (most recent call last):
  File "D:\swDefault\python\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 40, in run
    module.run()
  File "D:\swDefault\python\lib\site-packages\cx_Freeze\initscripts\Console.py", line 37, in run
    exec(code, {'__name__': '__main__'})
  File "D:\py\test.py", line 6, in <module>
    runway = cdll.LoadLibrary(os.path.join(os.getcwd(),"Runway.dll"))
  File "D:\swDefault\python\lib\ctypes\__init__.py", line 427, in LoadLibrary
    return self._dlltype(name)
  File "D:\swDefault\python\lib\ctypes\__init__.py", line 349, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] 找不到指定的模块。

运行时报错OSError: [WinError 126] 找不到指定的模块。

项目内使用了ctypes模块,加载了名为Runway.dll的文件。 我尝试将DLL移动到exe同目录下,启动依然报错。在这里找到了解决办法,于上一步生成的setup.py内加上include_files= ["Runway.dll"]。修改后的setup.py文件如下:

from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need
# fine tuning.

buildOptions = dict(packages = [], excludes = [], include_files= ["Runway.dll"]) #加在这里

base = 'Console'
executables = [
    Executable('D:\\py\\test.py', base=base, targetName = 'test.exe')
]
setup(name='',
      version = '1.0',
      description = '',
      options = dict(build_exe = buildOptions),
      executables = executables)

  • 4.cmd运行 python setup.py build

重新生成了test.exe,再次打开一切正常,问题解决。

这次主要就是ctypes引入了外部dll,本以为放在同目录下就没事了,结果发现还需要加上include_files= ["dll名字"]

踩坑记录

  • 1.ImportError: No module named 'scipy.spatial.cdtree’
    \lib\scipy\spatial目录下把文件名cKDTree.cp35-win_amd64.pyd 改成 ckdtree.cp35-win_amd64.pyd,我好奇为什么会出现这种大小写错误解决方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值