首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 23-09-22 13:03
  • 4413
  • 7385
blog.csdn.net
  • 专栏:Python基础教程目录
  • 专栏:使用PyQt开发图形界面Python应用
  • 专栏:PyQt入门学习
  • 老猿Python博文目录

在开发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对应的目录moviepyaudiofxall、moviepyvideofxall下的包文件__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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

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

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

如针对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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

输出如下:
在这里插入图片描述
将下面的输出语句拷贝到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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

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

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

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

此时再执行打包命令:
pyinstaller Video2Gif.py -w -p F:studypythonprojectloadApp
就可以打包后在生成的目录中直接执行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以上版本存在问题,具体问题及解决方案请参考《http://iyenn.com/rec/324881.html 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!

  • 专栏:Python基础教程目录
  • 专栏:使用PyQt开发图形界面Python应用
  • 专栏:PyQt入门学习
  • 老猿Python博文目录
文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览333550 人正在系统学习中
老猿Python
微信公众号
专注Python相关语言、图像音视频处理、AI
注:本文转载自blog.csdn.net的LaoYuanPython的文章"https://blog.csdn.net/LaoYuanPython/article/details/105346224"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top