Using Python 3.9
import bleak
import pyttsx3
engine = pyttsx3.init()
OSError: [WinError -2147417850] Cannot change thread mode after it is set
Related
import os
import re
import fitz # requires fitz, PyMuPDF
import pdfrw
import subprocess
import os.path
import sys
from PIL import Image
In my case fitz doesn't exist since it needs PyMuPDF. Is there a way to tell python to download dependencies and install them if they don't exist?
I am new to python and learning a lot. Apologizes in advance
Using Python 3.9.4
Platform that I am developing on is macOS but will be deploying on Windows
Editor is VSCode
Using try-catch to handle missing package
Ex:
import subprocess
def install(package):
subprocess.call(['pip', 'install', package])
try:
import fitz # requires fitz, PyMuPDF
except:
install('fitz')
A better practice would be to handle this before your code is executed. Example: using a requirements.txt file with all the dependent packages. And running the file before code execution.
Trying to convert a .py file into a .exe
when trying to run the .exe file in the exe.win32-3.6 folder I get the following error:
C:\Users\Aktan\Desktop\build\exe.win32-3.6>StatisticsCalculator.exe
Fatal Python error: Py_Initialize: unable to load the file system codec
Traceback (most recent call last):
File "C:\Users\Aktan\AppData\Local\Programs\Python\Python36-32\lib\encodings\__init__.py", line 31, in <module>
ModuleNotFoundError: No module named 'codecs'
here is my setup.py code:
import cx_Freeze
import sys
import os
import matplotlib
os.environ['TCL_LIBRARY'] = "C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tcl8.6"
base = None
if sys.platform == 'win32':
base='Win32GUI'
executables = [cx_Freeze.Executable("StatisticsCalculator.py", base=None)]
cx_Freeze.setup(
name="This is a tes",
options = {"build_exe": {"packages":["numpy"]}},
version = "0.01",
description = "Trying to get this to work",
executables = executables
)
and I do not know if it helps, but here are the modules I use in my python program:
import sqlite3
from math import pow, sqrt
from tkinter import Tk, Label, Listbox, END, Button, Message, messagebox
import matplotlib.pyplot as plt
I have python 3.6.3 and I am running Windows 10. Any response would be appreciated.
This is a known issue with cx_Freeze which has been resolved in the source. A new release (5.1.1) will be out shortly to correct that issue!
Is Py2exe just not supported for Python3 or is there something wrong with
build_exe my_script.py
The imports I made in my_script.py are as follows:
import os
import sys
import getpass
import hashlib
import platform
import base64
from cryptography.fernet import Fernet
But according to the documentation on Py2exe.org they say that they use an automatic module finder so you needn't worry about specific imports or whatever.
Can't figure out why I keep getting these errors
Unfortunately as of November 2017 there is no Python 3.5 support for py2exe.
I'm using pygattlib to interface with Bluetooth device in Python.
It works just fine, but the problem occurs when I try to use DBus in my code.
If I import gattlib and GLib.MainLoop().run(), the program freezes when calling run().
I found that gattlib has its own MainLoop for internal async calls.
I didn't find anything that would cover this specific issue, just some examples of having multiple mainloops in C.
This is the relevant part of the Python code:
import gattlib as bt
try:
from gi.repository import GLib
except ImportError:
import glib as GLib
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
[...do stuff with gattlib here...]
DBusGMainLoop(set_as_default=True)
dbusService = SystemDBus()
try:
loop = GLib.MainLoop()
loop.run() # it stalls here, doesn't respond to DBus or anything
except KeyboardInterrupt:
GLib.MainLoop().quit()
windows 10-64bit
I'm trying to use some text-to-speech tool to read text from lines of .txt document, something like this:
so with pyttsx:
import pyttsx
engine = pyttsx.init()
engine.say('my voice')
engine.runAndWait()
I got this error:
Traceback (most recent call last):
File "...", line 1, in <module>
import pyttsx
File "/.../pyttsx/__init__.py", line 18, in <module>
from engine import Engine
ImportError: No module named 'engine'
now gTTS, available as gtts_token, so how to use it? because this way module is unrecognizable:
import gtts
blabla = ("my voice")
tts = gtts.gTTS(text=blabla, lang='en')
tts.save("C:/rec.mp3")
or:
from gtts import gTTS
blabla = ("my voice")
tts = gTTS(text=blabla, lang='en')
tts.save("C:/rec.mp3")
error:
import gtts
ImportError: No module named 'gtts'
also I'm want try to use espeak but not sure how to install it, is it available with pip install or I have to install it some other way to try it:
import subprocess
text = '"my voice"'
subprocess.call('espeak '+text, shell=True)
or:
import os
os.system("espeak 'my voice'")
so I'm trying to find some solution, but everything I tried is not working here...
for python3 use
pyttsx3
Its a new library compatible with both python3 and python2. Unlike gTTS it doesn't need internet connection and there is no delay in the sound produced.
Install:
pip install pyttsx3
Usage :
import pyttsx3
engine = pyttsx3.init()
engine.say("Hi this is working ");
engine.setProperty('volume',0.9)
engine.runAndWait()
I am using windows 10 and Python 2.7.
For pyttsx:
Below code is working fine for me. I did get ImportError: No module named win32api error for which I had to install win32api from here
After that I could play "my voice". Although the quality and fidelity of spoken sound was very low. gtts is much better in that regards.
import pyttsx
engine = pyttsx.init()
engine.say('my voice')
engine.runAndWait()
For the error you are getting, Can you look into your python folder and see if engine.py file is present?
For e.g. in my case, I've pyttsx modules installed at following location
C:\Python27\Lib\site-packages\pyttsx and here is a list of files,
Name
----
drivers
driver.py
driver.pyc
engine.py
engine.pyc
voice.py
voice.pyc
__init__.py
__init__.pyc
Since import of engine is failing, I am wondering if you have engine.py file in the correct folder or present at all.
For gtts:
I tried playing sound with winsound, but it did not work. Using pydub I was able to play the audio file. But, since your requirement is not to use a file, this may be a moot point.
import gtts
import winsound
from pydub import AudioSegment
from pydub.playback import play
blabla = ("my voice")
tts = gtts.gTTS(text=blabla, lang='en')
tts.save("rec.mp3")
print "Playing sound .."
#winsound.PlaySound("rec.wav", winsound.SND_FILENAME)
song = AudioSegment.from_mp3("rec.mp3")
play(song)
Hope this helps.
I'm using python2.7 on Ubuntu.
Try to replace "from engine import Engine" with "from .engine import Engine" in the engine module.It work for me!