moviepy应用pyinstaller打包后执行报错AttributeError: module audio/video.fx.all has no attribute fadein、crop

解决moviepy打包问题
本文详细介绍了在使用PyInstaller打包包含moviepy库的Python程序时遇到的AttributeError问题及其解决方案,涉及动态模块加载和代码调整。

在开发moviepy的Python程序使用pyinstaller打包后执行,报了两个错:

  1. AttributeError: module ‘moviepy.video.fx.all’ has no attribute ‘crop’
  2. AttributeError: module ‘moviepy.audio.fx.all’ has no attribute ‘audio_fadein’

这两个错是因为moviepy包下子包audio.fx.all、video.fx.all对应的目录moviepy\audio\fx\all、moviepy\video\fx\all下的包文件__init__.py中,分别使用了如下的方式import模块:
audio.fx.all子包

import pkgutil

import moviepy.audio.fx as fx

__all__ = [name for _, name, _ in pkgutil.iter_modules(
    fx.__path__) if name != "all"]

for name in __all__:
    exec("from ..%s import %s" % (name, name))

video.fx.all子包

import pkgutil

import moviepy.video.fx as fx

__all__ = [name for _, name, _ in pkgutil.iter_modules(
    fx.__path__) if name != "all"]

for name in __all__:
    exec("from ..%s import %s" % (name, name))

可以看到这两个子包使用的是一种动态加载模块的模式加载包下的模块的,pyinstaller对这种模式不能处理。

解决办法:
1、将这2个__init__.py文件的上面所列最后两行注释掉;
2、在Python中将上述注释掉两行代码后前面的内容复制后加一个如下输出语句的代码:

for name in __all__: 
	print("from  moviepy.video.fx import %s" % (name))

如针对audio.fx.all,在Python中手工执行如下代码:

import pkgutil

import moviepy.video.fx as fx

__all__ = [name for _, name, _ in pkgutil.iter_modules(
    fx.__path__) if name != "all"]
for name in __all__: 
	print("from  moviepy.video.fx import %s" % (name))

输出如下:
在这里插入图片描述
将下面的输出语句拷贝到audio.fx.all的__init__.py,替换被注释的2行代码即可,最后完整的文件内容如下:

"""
Loads all the fx !
Usage:
import moviepy.video.fx.all as vfx
clip = vfx.resize(some_clip, width=400)
clip = vfx.mirror_x(some_clip)
"""

import pkgutil

import moviepy.video.fx as fx

__all__ = [name for _, name, _ in pkgutil.iter_modules(
    fx.__path__) if name != "all"]

#for name in __all__:
#    exec("from ..%s import %s" % (name, name))
#for name in __all__: print("from  moviepy.video.fx import %s" % (name))

from  moviepy.video.fx import accel_decel
from  moviepy.video.fx import blackwhite
from  moviepy.video.fx import blink
from  moviepy.video.fx import colorx
from  moviepy.video.fx import crop
from  moviepy.video.fx import even_size
from  moviepy.video.fx import fadein
from  moviepy.video.fx import fadeout
from  moviepy.video.fx import freeze
from  moviepy.video.fx import freeze_region
from  moviepy.video.fx import gamma_corr
from  moviepy.video.fx import headblur
from  moviepy.video.fx import invert_colors
from  moviepy.video.fx import loop
from  moviepy.video.fx import lum_contrast
from  moviepy.video.fx import make_loopable
from  moviepy.video.fx import margin
from  moviepy.video.fx import mask_and
from  moviepy.video.fx import mask_color
from  moviepy.video.fx import mask_or
from  moviepy.video.fx import mirror_x
from  moviepy.video.fx import mirror_y
from  moviepy.video.fx import painting
from  moviepy.video.fx import resize
from  moviepy.video.fx import rotate
from  moviepy.video.fx import scroll
from  moviepy.video.fx import speedx
from  moviepy.video.fx import supersample
from  moviepy.video.fx import time_mirror
from  moviepy.video.fx import time_symmetrize

同样对moviepy\video\fx\all下的包文件__init__.py进行类似处理。这样处理后,再重新打包就可以解决掉该问题。

上述处理过程如果担心破坏原始的moviepy包,可以将python目录下的moviepy包拷贝一份到对应代码目录下再修改更好。

如老猿的moviepy安装目录如下:
在这里插入图片描述
将其复制一份到应用代码目录后应用代码目录结构如下:
在这里插入图片描述

此时再执行打包命令:
pyinstaller Video2Gif.py -w -p F:\study\python\project\loadApp
就可以打包后在生成的目录中直接执行Video2Gif.exe即可正常运行。

不过老猿使用PyQt开发的界面调用moviepy打包还是遇到了一点问题:
当同时使用-F和-w选项时,生成的不带其他文件的单一文件Video2Gif.exe执行时报错:
在这里插入图片描述
反复测试发现:
1、去掉 -F -w中任意一个都能正常执行生成的exe文件,不过去掉-F时是生成的一个带很多文件的子目录(包括要生成的exe文件),不是单一EXE文件,去掉-w时,会生成单一exe文件,但执行时除了图形界面窗口外还会出现控制台窗口,如图:
在这里插入图片描述
2、将代码中与moviepy相关的代码注释掉后,带 -F -w执行打包生成的exe文件能正常执行。
以上问题老猿还没找到解决办法,不过去掉 -F -w中任意一个打包的方式生成的文件也可用,因此也无需过于纠结。

修订说明:

本文部分内容存在错误,具体问题请见《关于moviepy打包报错AttributeError: module audio/video.fx.all has no attribute fadein、crop文章的纠错和抄袭

另外,本文的解决办法在Python3.8以上版本存在问题,具体问题及解决方案请参考《https://blog.youkuaiyun.com/LaoYuanPython/article/details/114385758 Python 3.8+moviepy报错:TypeError: ‘module‘ object is not callable》。

更多moviepy的介绍请参考《PyQt+moviepy音视频剪辑实战文章目录》或《moviepy音视频开发专栏》。

关于收费专栏

老猿的付费专栏《使用PyQt开发图形界面Python应用》专门介绍基于Python的PyQt图形界面开发基础教程,付费专栏《moviepy音视频开发专栏》详细介绍moviepy音视频剪辑合成处理的类相关方法及使用相关方法进行相关剪辑合成场景的处理,两个专栏加起来只需要19.9元,都适合有一定Python基础但无相关专利知识的小白读者学习。

对于缺乏Python基础的同仁,可以通过老猿的免费专栏《专栏:Python基础教程目录》从零开始学习Python。

如果有兴趣也愿意支持老猿的读者,欢迎购买付费专栏。

老猿Python,跟老猿学Python!

<think>我们正在分析用户的问题:用户在使用moviepy时遇到了错误`AttributeError: module 'moviepy.video.fx.all' has no attribute 'reverse'`,希望知道原因。 根据用户提供的引用内容,虽然它们主要涉及OpenCV和图像处理,但我们可以从中获得一些关于模块属性错误的普遍原因。例如,引用[1]中提到,当某个模块没有某个属性时,可能是因为该功能在特定版本中已被移除或需要额外的包。 针对moviepy的错误,我们可以进行如下推理: 1. **功能位置变更**:在moviepy的不同版本中,`reverse`函数可能被移动到了其他模块。 2. **版本问题**:用户安装的moviepy版本可能过旧或过新,导致某些功能不存在或被移除。 3. **导入方式错误**:用户可能使用了错误的导入方式。 我们查阅moviepy的官方文档或常见问题,可以得知: - 在moviepy中,视频特效(fx)函数可以通过`video.fx`模块访问,但具体到`reverse`函数,它实际上是`moviepy.video.fx.reverse`模块中的一个函数。因此,我们不能直接从`moviepy.video.fx.all`中导入`reverse`,因为`all`模块可能并不包含所有的特效函数。 实际上,在moviepy中,推荐使用以下方式之一来使用`reverse`函数: - 方式1:通过`VideoClip`的`fx`方法:`clip.fx(vfx.reverse)` - 方式2:直接导入:`from moviepy.video.fx import reverse`,然后使用`reverse(clip)` 因此,错误的原因可能是用户尝试了类似`moviepy.video.fx.all.reverse`的调用方式,而`all`模块并没有包含`reverse`函数。 具体原因分析: - 在moviepy中,`moviepy.video.fx.all`模块实际上是一个动态导入的模块,它包含了一些常用的视频特效,但并不是全部特效。而`reverse`特效可能没有被包含在`all`模块中。 - 另外,在moviepy的某些版本中,`all`模块可能被重新组织,导致部分函数不可用。 解决方法: 1. 使用正确的导入方式: ```python from moviepy.video.fx import reverse reversed_clip = reverse(clip) ``` 或者 ```python reversed_clip = clip.fx(reverse) ``` 2. 检查moviepy版本:确保安装的moviepy版本支持该函数。可以通过`pip show moviepy`查看版本,并考虑升级到最新版本(如果版本过旧)或回退到稳定版本(如果最新版本有变更)。 引用用户提供的参考资料,虽然它们不是直接关于moviepy的,但引用[1]中提到的因为专利问题导致模块移除的情况,也提醒我们可能是版本升级导致某些功能被移除。因此,检查版本和文档是必要的。 总结错误原因: - `moviepy.video.fx.all`模块中确实没有`reverse`属性,因为`reverse`是一个单独的函数,位于`moviepy.video.fx.reverse`模块中,并且可能没有被包含在`all`模块中。 因此,用户需要更改导入方式。 考虑到用户可能需要更多信息,我们可以生成相关问题,帮助用户深入理解。 </think>根据提供的引用信息和常见原因分析,`AttributeError: module 'moviepy.video.fx.all' has no attribute 'reverse'` 错误的主要原因如下: ### 核心原因 **1. 版本兼容性问题(最常见原因)** - 在 MoviePy `1.0.0` 及更高版本中,`reverse()` 函数已从 `video.fx.all` 模块移除[^1] - 旧版本导入方式: ```python from moviepy.video.fx.all import reverse # ❌ 新版本失效 ``` - 新版本需改用命名空间导入: ```python from moviepy.video.fx import reverse # ✅ 正确方式 ``` **2. 安装包不完整** - 类似引用[1]中OpenCV的SURF特征缺失问题,若MoviePy安装时依赖包不完整(如缺少`scikit-image`或`numpy`),可能导致部分功能无法加载[^1]。 **3. API用法变更** - MoviePy 从 `0.2.3` 升级到 `1.x` 时进行了重大API调整: ```python # 旧版本 (0.2.3) clip.fx(vfx.reverse) # 新版本 (1.0+) from moviepy.video.fx import reverse clip.fx(reverse) ``` ### 解决方案 1. **更新导入语句**: ```python # 错误方式 ❌ from moviepy.video.fx.all import reverse # 正确方式 ✅ from moviepy.video.fx import reverse reversed_clip = clip.fx(reverse) ``` 2. **检查版本兼容性**: ```bash pip show moviepy # 查看当前版本 ``` - 若版本 ≥1.0.0:必须使用新API - 若版本 ≤0.2.3:可暂时兼容旧语法但建议升级 3. **重安装完整依赖**: ```bash pip uninstall moviepy pip install "moviepy[optional]" # 安装所有可选依赖 ``` ### 版本差异对比表 | 特性 | MoviePy < 1.0.0 | MoviePy ≥ 1.0.0 | |---------------|--------------------------|--------------------------| | 导入路径 | `video.fx.all.*` | `video.fx.*` | | reverse调用 | `vfx.reverse` | 直接`reverse` | | 依赖管理 | 基础功能独立 | 需额外安装`scikit-image` |
评论 23
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LaoYuanPython

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值