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

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

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

Impossible to use pyttsx3: No module named 'pywintypes', but pywin32 is installed

I try to use speech synthesis in Python 3.9.13 [MSC v.1929 64 bit (AMD64)].
If I understand well, pyttxs3 is the dedicated module (thank you in advance for any possible alternative!), which I installed successfully:
c:\>pip install -U pyttsx3
Requirement already satisfied: pyttsx3 in c:\users\...\python39\site-packages (2.90)
Requirement already satisfied: comtypes in c:\users\...\python39\site-packages (from pyttsx3) (1.1.11)
Requirement already satisfied: pywin32 in c:\users\...\python39\site-packages (from pyttsx3) (304)
Requirement already satisfied: pypiwin32 in c:\users\...\python39\site-packages (from pyttsx3) (223)
but I cannot initialize the engine: when I do
>>> e = pyttsx3.init()
Traceback (most recent call last):
File "C:\Users\...\Python39\site-packages\pyttsx3\__init__.py", line 20, in init
eng = _activeEngines[driverName]
File "C:\Program Files\...\lib\weakref.py", line 137, in __getitem__
o = self.data[key]()
KeyError: None
During handling of the above exception, another exception occurred:
...
File "C:\Users\...\Python39\site-packages\pyttsx3\drivers\sapi5.py", line 10, in <module>
import pythoncom
File "C:\Users\...\Python39\site-packages\pythoncom.py", line 2, in <module>
import pywintypes
ModuleNotFoundError: No module named 'pywintypes'
I noticed that part of the scripts are in my Users\...\AppData\Local\Packages\PythonSoftwareFoundation... and some are in "C:\Program Files\WindowsApps\PythonSoftwareFoundation.... I wonder whether that might be the reason for the errors.
How could I fix this? (I also found that there are other modules, speech and pyttsx, which however appear to be for Python 2.7: they produce Syntax error: print "..." - did you mean print("...")? Is there a Python 3 version of speech or any other alternative?)
pyttxs3 is a little (actually very) glitched for example after i removed a part of my code which involved speaking something it still spoke my old code i removed the whole pyttxs3 from my code it still didn't work at last i had to uninstall the package. I used this function. You need to install pygame (it is a game making package but we will use it) and gtts i.e google text to speech
import os
from pygame import *
from gtts import *
def speak(text: str):
try:
tts_ = gTTS(text=text, lang='en')
ran = random.randint(0, 1000000)
audio_file = 'audio-' + str(ran) + '.mp3'
tts_.save(audio_file)
# Starting the mixer
mixer.init()
# Loading the song
mixer.music.load("C:///Users/roopa/PycharmProjects/pokemon game/" + audio_file)
# Start playing the song
mixer.music.play()
clock = time.Clock()
# infinite loop
while mixer.music.get_busy():
clock.tick(60)
mixer.music.unload()
os.remove(audio_file)
except gTTSError:
print('unknown error')
Roop's answer didn't work out completely for me due to an error "Failed loading libmpg123-0.dll", but it enabled me to find the following working solution:
import gtts # text to mp3 file
import random # for random file name generation
import playsound # to play mp3 file
import os # to remove audio file
def say(text: str, block = True, lang = 'en'):
"""Text-to-speech synthesis using google TTS. If block=True,
waits until the text is spoken. If False, return a cleanup
function to delete the temporary audio file."""
audio_file = f'audio-{random.randint(0, 1000000)}.mp3'
gtts.gTTS(text, lang = lang).save(audio_file)
playsound.playsound(audio_file, block = block)
if block:
os.remove(audio_file)
else:
print(f"Playing sound, don't forget to do: os.remove('{audio_file}')"
"\n(You can do so by calling the returned function.)")
return(lambda: os.remove(audio_file))
if __name__=='__main__':
cleanup = say("Good morning, Max!")
if cleanup: # if block = True, function returns None
import time
time.sleep(10)
cleanup()
(It appears to be a known bug of playsound that the sound isn't played to the end if the script ends too early, therefore I added the sleep(10), but obviously you can do other stuff instead.)
That said, the question regarding pyttsx3 and pywin32 remains open.

Toast Notifier is not working, Have i done something wrong?

First off thanks for taking the time to read this:
I am currently trying to get ToastNotifier working in python in MU.
I have imported ToastNotifier from win10toast as such :
from win10toast import ToastNotifier
I have the created this function:
def notifacation():
toast = ToastNotifier()
Title = "Notifacation"
message = "Hello from Ben Colledge"
icon = "a.ico"
length = 30
toast.show_toast(title, message, icon_path=icon, duration=length)
And then called the function like so :
notifacation()
I then run the code and it give me this error message
Traceback (most recent call last):
File "c:\users\puzzl\mu_code\notifacation.py", line 1, in module
from win10toast import ToastNotifier
ModuleNotFoundError: No module named 'win10toast'
I have then gone to https://pypi.org/project/win10toast/ and downloaded win10toast yet nothing happened
Any help would be appreciated
Thanks
I had the exact same problem but then I figured out this works best
python.exe -m pip install win10toast
I ran this code and it still has a problem not, although, the same one. Now the bug would be that in the line that has the method.
toast.show_toast(title, message, icon_path=icon, duration=length)
The problem is that 'title' is undefined. The variable that you assigned was named 'Title'. Since Python is case-sensitive, 'title' is an undifined variable. this is what the line should look like now:
toast.show_toast(Title, message, icon_path=icon, duration=length)
Yay! It works now!

Import image in python

hello I am a beginner in python and I have problems executing my code .
how can i fix this error with python:
import cgitb
cgitb.enable()
print('Content-type: text/html\r\n')
print('\r\n')
import Image, ImageDraw
import sys
import math, random
from itertools import product
from ufarray import *
ModuleNotFoundError: No module named 'Image'
args = ("No module named 'Image'",)
msg = "No module named 'Image'"
name = 'Image'
path = None
with_traceback = <built-in method with_traceback of ModuleNotFoundError
Make sure you have installed Pillow (the supported, open-source version of the PIL Python Image Library) and then change your import to:
from PIL import Image, ImageDraw
That should get you farther along.
Try this:
from PIL import Image
or else you haven't installed it yet:
pip install pillow
I had a similar Problem, this post helped me: How to insert an image in python
What they basically use is:
import Image
myImage = Image.open("your_image_here");
myImage.show();
For more help I would need your full code. Even after the edit it is not quite clear to me what is your code, what is the error and what you are actually trying to do.

Can not import name from x in python 2.7

recently i have created this topic and did not get the answer.
after that i edited this and added 2 screenshots.
right now i tested another temporary email library and got the same error :((
this library :
pip install python-guerrillamail
code python 2.7:
from guerrillamail import GuerrillaMailSession
session = GuerrillaMailSession()
print session.get_session_state()['email_address']
print session.get_email_list()[0].guid
it seemed i cant never work like this email lib.
updated after remove email.pyc:
Traceback (most recent call last):
File "C:\Users\11\Desktop\untitle.py", line 1, in <module>
from guerrillamail import Guerrillamail
ImportError: cannot import name Guerrillamail
Your module email.py, is shadowing, or hiding, the email package in Python's standard library. This is causing the error: in the traceback you can see that an error is being reported when the statement import email is executed.
Rename your file to something else, for example myemail.py.
I changed the code to " from guerrillamail import * " that is worked. this code seems import all things to program, maybe it causes to be heavy and executing time.
thanks to #snakecharmerb .

NotImplementedError:font module not available

I have written the following codes in the setup file and include both sdl_ttf.dll", "SDL.dll in the default folder.
But, it shows an error message:
NotImplementedError:font module not available
<Import error: DLL load failed:can't find assigned module>
The code
from distutils.core import setup
import py2exe,sys,os
import pygame
setup(console=['blackjack.py'])
origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
if os.path.basename(pathname).lower() in ["sdl_ttf.dll", "SDL.dll"]:
return 0
return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL
pygamedir = os.path.split(pygame.base.__file__)[0]
os.path.join(pygamedir, pygame.font.get_default_font()),
os.path.join(pygamedir, 'SDL.dll'),
os.path.join(pygamedir, 'SDL_ttf.dll')
Is there something wrong?
Your check
if os.path.basename(pathname).lower() in ["sdl_ttf.dll", "SDL.dll"]:
will not work, since you're calling lower() on the filename, but use SDL.dll instead of sdl.dll, so py2exe will not include the sdl library.
You could also try to use this script from the pygame wiki.

Categories