NameError: name 'moviepy' is not defined after pip install moviepy

Viewed 469

I'm running into an issue with my moviepy install but I can't figure out where it is going wrong. I have tried pip install moviepy and it says all the requirements are satisfied, but in my editor when I try "from moviepy.editor import *" moviepy is underlined and says ""moviepy": Unknown word." I have tried running pip uninstall moviepy and reinstalling it but that hasn't worked. I'm using selenium in the same project and it works fine which is why I'm confused but if anyone has an idea of what to do I would really appreciate it.

Here is the code if needed, and if you want me to try running something let me know.

main.py

from get_clips import get_clips
from download_clips import download_clips
from upload_clips import upload_clips
from edit_clips import edit_clips

if __name__ == "__main__":
    clips = get_clips()
    download_clips(clips)
    edit_clips(clips)
    #upload_clips(clips)

    print('done')

edit_clips.py

from moviepy.editor import *

def edit_clips(clips):
    for clip in clips:
        video = moviepy.editor.VideoFileClip('D:/zgarw/Documents/Projects/autoclip/tmp/' + clip['slug'] + '.mp4')
        video_duration = int(video.duration)
        print(video_duration)

This is the error I'm getting when I run the code.

Traceback (most recent call last):
  File "c:\Users\zgarw\Documents\Projects\autoclip\main.py", line 9, in <module>
    edit_clips(clips)
  File "c:\Users\zgarw\Documents\Projects\autoclip\edit_clips.py", line 6, in edit_clips
    video = moviepy.editor.VideoFileClip('D:/zgarw/Documents/Projects/autoclip/tmp/' + clip['slug'] + '.mp4')
NameError: name 'moviepy' is not defined
2 Answers

You're getting the error because you haven't imported the name moviepy into the current namespace. Instead, you imported all the public members of moviepy.editor. Change your code to

video = VideoFileClip('D:/zgarw/Documents/Projects/autoclip/tmp/' + clip['slug'] + '.mp4')

and it should run fine. If you want to access other modules of moviepy or use your original code, simply do import moviepy.

If you are only using VideoFileClip then you can edit edit_clips.py as :

from moviepy.editor import VideoFileClip
def edit_clips(clips):
    for clip in clips:
        video = VideoFileClip('D:/zgarw/Documents/Projects/autoclip/tmp/' + clip['slug'] + '.mp4')
        video_duration = int(video.duration)
        print(video_duration)

Since using in general import statement as

from module import *

will pollute namespace and is a bad habit.

Related