目前需要打包一个python程序到一个没安装python环境的win平台上使用。查了查python打包exe的办法,对比后选择使用cxfreeze。
> 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
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 命令。
> 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
尝试使用cmd打开。
> 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
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)
重新生成了test.exe,再次打开一切正常,问题解决。
这次主要就是ctypes引入了外部dll,本以为放在同目录下就没事了,结果发现还需要加上include_files= ["dll名字"]
。
\lib\scipy\spatial
目录下把文件名cKDTree.cp35-win_amd64.pyd
改成 ckdtree.cp35-win_amd64.pyd
,我好奇为什么会出现这种大小写错误解决方法