Python showing me error of " No module named gTTS ." - macos-catalina

I wanted to create python code for Voice Assistance which will use gTTS.
I ran the code but it didn't work as it showed me,
Traceback (most recent call last):
File "/Users/niallquadros/Desktop/voiceassistant.py", line 1, in <module>
from gTTS import gTTS
ModuleNotFoundError: No module named 'gTTS'
Instead, it should have given me the result, and act as a voice assistant.
This was my code which I had written on MacBook Air 2019 on python (3.7.4)
from gTTS import gTTS
import speech_recognition as sr
import os
import webbrowser
import satplib
def talkToMe(audio):
print (audio)
tts = gTTs(text=audio, lang='en')
tts.save('audio.mp3')
os.system('mpg123 audio.mp3')
#Listen for commands
def myCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('I am ready for your next command')
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration = 1)
audio = r.listen(source)
try:
command = r.recognize_google(audio)
print('You said: ' + command + '/n')
#loop back to continue to listen for commands
except sr.UnknownValueError:
assistant(myCommand())
return command
#if statesments for executing commands
def assistant(command):
if 'open Reddit python' in command:
chrome_path = '/user/bin/google.chrome'
url = 'https://www.reddit.com/r/python'
webbrowser.get(chrome_path).open(url)
if 'what\'s up' in command:
talkToMe('Chillin Bro')
while True:
assistant(myCommand())
In my terminal, it shows me that I have gtts installed already.
(base) Nialls-MacBook-Air:~ niallquadros$ pip search gTTS
gTTS (2.1.0) - gTTS (Google Text-to-Speech), a Python library and CLI
tool to interface with Google Translate text-to-speech
API
INSTALLED: 2.1.0 (latest)
Django-Gtts (0.2) - gTTS google text-to-speech django app
Flask-gTTS (0.12) - gTTS Google text to speech flask extension
gTTS-token (1.1.3) - Calculates a token to run the Google Translate text to
speech
INSTALLED: 1.1.3 (latest)
wired-tts (0.2.0) - "gTTS based Wired story reader."
What to do, so that I can execute the code?

I hope you have first installed the library(Google Text-to-Speech) If not please install it from following command in python:
pip install gTTs
After you have installed the library its ready to use in your code be careful about the case of the character.
from gtts import gTTs
Hope this will help you!

Try this:
from gtts import gTTS

Try:
from gtts import *
It will import everything. This worked for me.

make sure you have it installed with the correct pips. So if you're running it with python3 then it needs to be installed on pip3..

Ah that's because your file is named gtts.py so when you do from gtts import gTTS, Python is trying to import the file you're currently running.
Try renaming your script to something else, should work!

Instead of:
from gTTS import gTTS
Write the following command:
from gtts import gTTS

If above answers are not solving your problem then the issue might be about IDE (Integrated Development Environment).
Suppose that if you are using IDE like Anaconda and working on Jupyter notebook then you have to install gtts within the environment of Anaconda.
Just go to the Anaconda navigator and open CMD.exe Prompt
type: pip install gTTS or pip3 install gTTS pyttsx3 playsound
Now, you can import gtts.

Related

Flask-mongoengine: Unable to import MongoEngine From flask-mongoengine

I must be missing something but I look around and couldn't find reference to this issue.
I have the very basic code, as seen in flask-mongoengine documentation.
test.py:
from flask import Flask
from flask_mongoengine import MongoEngine
When I run
python test.py
...
from flask_mongoengine import MongoEngine
ImportError: cannot import name 'MongoEngine'
Module in virtual environment contain (requirements.txt):
click==6.7
Flask==1.0.2
flask-mongoengine==0.9.5
Flask-WTF==0.14.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
mongoengine==0.15.3
pymongo==3.7.1
six==1.11.0
Werkzeug==0.14.1
WTForms==2.2.1
My interpreter is Python 3.6.5
Any help would be appreciated. Thanks.
Since your using a virtual environment did you try opening your editor from your virtual environment?
For example opening the vscode editor from command-line is "code". Go to your virtual environment via the terminal and activate then type "code" at your prompt.
terminal:~path/to/virtual-enviroment$ source bin/activate
(virtual-enviroment)terminal:~path/to/virtual-enviroment$ code
If that doesn't work I, myself, haven't used flask-mongoengine. I was nervous of any issues that would come from the abstraction of it and instead just used Mongoengine with Flask.
I'm assuming you're only using this library for connection management so if you can't solve your issue with flask-mongoengine but are still interested in using mongoengine this was my approach. ~
I would put this in a config file somewhere and import it where appropriate-
from flask import Flask
MONGODB_DB = 'DB_NAME'
MONGODB_HOST = '127.0.0.1' # or whatever your db address
MONGODB_PORT = 27017 # or whatever your port
app = Flask(__name__) # you can import app from config and it will keep its configurations
then I would connect and disconnect from the database within each HTTP request function like this-
from config import MONGO_DB, MONGODB_HOST, MONGODB_PORT
# to connect
db = connect(MONGODB_DB, host=MONGODB_HOST, port=MONGODB_PORT)
# to close connection before any returns
db.close()
Hope this helps.
I had this issue and managed to fix it by deactivating, reinstalling flask-mongoengine and reactivating the venv (all in the Terminal):
deactivate
pip install flask-mongoengine
# Not required but good to check it was properly installed
pip freeze
venv\Scripts\activate
flask run

create_engine in sqlalchemy not working in python 3.6 runtime for aws lambda

I successfully tested pandas, numpy, and sqlalchemy in an Amazon Linux docker image using python 3.6. I was able to import, use, and connect to a database in the virtual environment using create_engine from the sqlalchemy module in python 3.6.
I then exported all the dependencies and built a python deployment package to run it in AWS Lambda but for some reason I keep getting an error for create_engine in lambda.
module 'sqlalchemy' has no attribute 'create_engine': AttributeError
This is my code:
import pandas as pd
import numpy as np
import sqlalchemy
from datetime import datetime, timedelta
def lambda_handler(event, context):
engine = sqlalchemy.create_engine("DB_URI")
return "Hello world!"
However, if I simply comment out the line where I call create_engine, I get my "Hello world!" response.
I don't get why create_engine is not working in this environment when it worked perfectly fine in the identical docker environment. Any ideas?
I figured it out. I had a rookie mistake when I was zipping up my file and didn't use the -r option which meant only the top level of my python module folders where getting zipped up. This explains why I wasn't getting an import error but none of the actual methods were working.
So to reiterate, the solution was adding the -r option to my zip operation to add all the files recursively:
zip -r package.zip *

Odoo custom module with external Python library

I created an Odoo Module in Python using the Python library ujson.
I installed this library on my development server manually with pip install ujson.
Now I want to install the Module on my live server. Can I somehow tell the Odoo Module to install the ujson library when it is installed? So I just have to add the Module to my addons path and install it via the Odoo Web Interface?
Another reason to have this automated would be if I like to share my custom module, so others don't have to install the library manually on their server.
Any suggestions how to configure my Module that way? Or should I just include the library's directory in my module?
You should try-except the import to handle problems on odoo server start:
try:
from external_dependency import ClassA
except ImportError:
pass
And for other users of your module, extend the external_dependencies in your module manifest (v9 and less: __openerp__.py; v10+: __manifest__.py), which will prompt a warning on installation:
"external_dependencies": {
'python': ['external_dependency']
},
Big thanks goes to Ivan and his Blog
Thank you for your help, #Walid Mashal and #CZoellner, you both pointed me to the right direction.
I solved this task now with the following code added to the __init__.py of my module:
import pip
try:
import ujson
except ImportError:
print('\n There was no such module named -ujson- installed')
print('xxxxxxxxxxxxxxxx installing ujson xxxxxxxxxxxxxx')
pip.main(['install', 'ujson'])
In python file using the following command, you can install it (it works for odoo only). Eg: Here I am going to install xlsxwriter
try:
import xlsxwriter
except:
os.system("pip install xlsxwriter")
import xlsxwriter
The following is the code that is used in odoo base module report in base addons inside report.py (odoo_root_folder/addons/report/models/report.py) to install wkhtmltopdf.
from openerp.tools.misc import find_in_path
import subprocess
def _get_wkhtmltopdf_bin():
return find_in_path('wkhtmltopdf')
try:
process = subprocess.Popen([_get_wkhtmltopdf_bin(), '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except (OSError, IOError):
_logger.info('You need Wkhtmltopdf to print a pdf version of the reports.')
basically you need to find some python code that will run the library and install it and include that code in one of you .py files, and that should do it.

unresolved import gcs_oauth2_boto_plugin

I am currently trying to create bucket in the Google Cloud Storage using Python and the documentation provided by Google here at this link.
https://cloud.google.com/storage/docs/gspythonlibrary
I have followed the instructions and I have successfully install the stand alone gsutil. However, once I go into eclipse and import gcs_oauth2_boto_plugin, it does not recognize it even though it recognizes the import boto.
It was a problem with both my PYTHON PATH and Eclipse. What I ended up doing was
import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages")
try:
import boto
from boto import connect_gs
except:
print 'neither of the modules were imported'
This solved my problem. Updating the python path did not.

ImportError: No module named Selenium, any suggestions?

I am in the process of learning the RobotFramework from an absolute beginners point.
I have had experience creating automation scripts using ruby-watir and gherkin. As well as automation in c#.
However I am simply stumped with even the setup of RobotFramework in Pycharm.
I have installed all the packages
I deemed necessary
-including selenium/RobotFramework /RobotFramework-selenium2library.
My Python.exe and scripts are in the correct directory as well as the environment variables being added.
However when I run :
from lettuce import *
from behave import *
from Selenium import webdriver
#step("I am on the web")
def step_impl(context):
context.browser = webdriver.Firefox
pass
I get:
Traceback (most recent call last):
File "C:/Users/Jordan/PycharmProjects/RbtFrameWork/features/steps/Test_steps.py", line 3, in <module>
from Selenium import webdriver
ImportError: No module named Selenium
I imagine it's something to do with the installation location of my selenium - which I have installed through pip
Any help?
Python is case sensitive so instead of from Selenium import webdriver you should use from selenium import webdriver