Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.4k stars 1.55k forks source link

ImportError: cannot import name '__pycache__' from 'moviepy.video.fx.__pycache__' #1701

Open Shubham-Kumar-2000 opened 2 years ago

Shubham-Kumar-2000 commented 2 years ago

I am trying to deploy a simple application on aws lambda using zappa but I get this error every time.

Instancing..
[1639322044759] /var/task/bin/ffmpeg
[1639322044881] OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
[1639322047418] [ERROR] ImportError: cannot import name '__pycache__' from 'moviepy.video.fx.__pycache__' (/var/task/moviepy/video/fx/__pycache__/__init__.py)
Traceback (most recent call last):
  File "/var/task/handler.py", line 657, in lambda_handler
    return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 251, in lambda_handler
    handler = cls()
  File "/var/task/handler.py", line 148, in __init__
    self.app_module = importlib.import_module(self.settings.APP_MODULE)
  File "/var/lang/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/var/task/app.py", line 10, in <module>
    from trans.extract import extract_text
  File "/var/task/trans/extract.py", line 6, in <module>
    import moviepy.editor as mp
  File "/var/task/moviepy/editor.py", line 48, in <module>
    import moviepy.video.fx.all as vfx
  File "/var/task/moviepy/video/fx/all/__init__.py", line 17, in <module>
    exec("from ..%s import %s" % (name, name))
  File "<string>", line 1, in <module>

I am new to python thus don't know what exactly this means. Any help is appreciated.

davmixcool commented 1 year ago

Hi @Shubham-Kumar-2000, were you able to get around this error?

davmixcool commented 1 year ago

So, I ended up editing the moviepy package code directly and ignoring the pycache folder from the import loop inside /path/to/moviepy/video/fx/all/__init__.py and /path/to/moviepy/audio/fx/all/__init__.py then compressing the files and uploading back to AWS Lambda

Example code for /path/to/moviepy/video/fx/all/__init__.py

"""
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" and name != "__pycache__"]

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

You can do the same for /path/to/moviepy/audio/fx/all/__init__.py

Hope this helps someone looking for a quick hacky fix.

Shubham-Kumar-2000 commented 1 year ago

for me it was happening while using slim: true in zappa to deploy