cxfreeze打包python项目踩坑笔记

先说结论

  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,我好奇为什么会出现这种大小写错误解决方法
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
打包Python项目有多种方法和技术可以实现。其中一种常见的方法是使用工具如setuptools和distutils来创建项目打包文件。以下是一个简单的步骤来打包Python项目: 1. 首先,确保你的项目结构是正确的,包括主要代码文件、依赖库文件和其他必要的资源文件。 2. 创建一个setup.py文件,它是一个用于描述项目打包信息的Python脚本。在这个文件中,你需要定义项目的名称、版本号、作者等基本信息,并指定项目的入口文件和依赖库。例如: ``` from setuptools import setup setup( name='your_project_name', version='1.0', author='your_name', packages=['your_package'], install_requires=['dependency1', 'dependency2'], ) ``` 3. 执行以下命令安装setuptools,如果你还没有安装的话:`pip install setuptools` 4. 运行以下命令来构建和打包你的项目:`python setup.py sdist bdist_wheel` 5. 在你的项目目录下,将会生成一个dist文件夹,里面包含着打包后的文件。 6. 如果你想将你的项目发布到PyPI,***ine upload dist/*` 除了使用setuptools和distutils,还有其他一些工具和技术可以用来打包Python项目,比如使用PyInstaller来创建可执行的独立应用程序,使用virtualenv来创建一个虚拟环境以隔离项目的依赖等等。选择哪种方法取决于你的具体需求和项目的特点。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值