bashkash
bashkash

Reputation: 57

cannot import gTTS from gtts:IMPORT ERROR

So this speech to text code in python is having problem in import gTTs when it is imported gtts only then its fine but the problem persists with gTTS. The code is as belows.

from gtts import gTTs

import os

text_to_read = "Read any text written "
language = 'en'
slow_audio_speed = False
filename = 'myfile.mp3'

def reading_from_string():
      audio_created = gtts.gTTs(text=text_to_read,lang = language,slow = slow_audio_speed)

      audio_created.save("myfile.mp3")
      os.system("mpg321 myfile.mp3")

if __name__ == "__main__":
     reading_from_string()

the error is as belows

*ImportError: cannot import name 'gTTs' from 'gtts' * i tried to uninstall and install pip gtts and pip gTTs again and again but the problem seems to be with gTTs. Also if possible can you suggest a solution to add some natural voice in this code to make it sound more natural

Upvotes: 0

Views: 812

Answers (1)

bipin_s
bipin_s

Reputation: 465

this should work.

from gtts import gTTS
import os
text_to_read = "Read any text written "
language = 'en'
slow_audio_speed = False
filename = 'myfile.mp3'
def reading_from_string():
      audio_created = gTTS(text=text_to_read,lang = language,slow = slow_audio_speed)
      audio_created.save("myfile.mp3")
      os.system("start myfile.mp3")
if __name__ == "__main__":
     reading_from_string()

first it should be gTTS and not gTTs (note small and caps 'S') and in function reading_from_string() it should be gTTS(...) and not gtts.gTTs

Upvotes: 2

Related Questions