I am trying to convert text to speech in Python 3.10.2 using the code:
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello, it works!")
But there is continuously occuring an error:
Traceback (most recent call last): File "d:\Program\Python programing\tempCodeRunnerFile.py", line 1, in <module>
import win32com.client File "C:\Users\ASUS\AppData\Roaming\Python\Python310\site-packages\win32com\client\__init__.py",
line 10, in <module>
from . import dynamic File "C:\Users\ASUS\AppData\Roaming\Python\Python310\site-packages\win32com\client\dynamic.py",
line 24, in <module>
from . import build File "C:\Users\ASUS\AppData\Roaming\Python\Python310\site-packages\win32com\client\build.py",
line 638, in <module>
valid_identifier_chars = string.ascii_letters + string.digits + "_" AttributeError: module 'string' has no attribute 'ascii_letters'
you wrote two lines of code in the same line
Try this
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello, it works!")
or try to put a semicolon after the first line of code
Try this
import win32com.client; speaker =win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello, it works!")
either way should work
Related
I'm getting this error when trying to using polyglot library for the first time.
from polyglot.text import Text, Word
word = Text("Preprocessing is an essential step.").words[0]
print(word.morphemes)
here is the full error message.
Traceback (most recent call last):
File "/home/adeesha/PycharmProjects/theme_recomend/plyglot.py", line 2, in <module>
from polyglot.text import Text, Word
File "/usr/local/lib/python3.5/dist-packages/polyglot-16.7.4-py3.5.egg/polyglot/__init__.py", line 12, in <module>
from .base import Sequence, TokenSequence
File "/usr/local/lib/python3.5/dist-packages/polyglot-16.7.4-py3.5.egg/polyglot/base.py", line 9, in <module>
from concurrent.futures import ProcessPoolExecutor, as_completed
ImportError: cannot import name 'ProcessPoolExecutor'
the version of Python 3.5.2
how I gonna solve this problem?
I am new to Python 3.4.5 which I am learning online by watching videos with some good knowledge of C. I am trying to download an image via Python which I am unable to do because of this error.
Code:
import random
import urllib.request
def img(url):
full='name'+'.jpeg'
urllib.request.urlretrieve(url,full)
img("http://lorempixel.com/400/200")
Error:
Traceback (most recent call last):
File "image.py", line 2, in <module>
import urllib.request
File "/home/yuvi/pyth/urllib/request.py", line 88, in <module>
import http.client
File "/usr/local/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/local/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/local/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/local/lib/python3.4/email/message.py", line 16, in <module>
from email import utils
File "/usr/local/lib/python3.4/email/utils.py", line 31, in <module>
import urllib.parse
File "/home/yuvi/pyth/urllib/parse.py", line 239, in <module>
_DefragResultBase.url.__doc__ = """The URL with no fragment identifier."""
AttributeError: readonly attribute
Try:
def img(url): full='name'+'.jpeg';urllib.urlretrieve(url,full)
urllib.request does not exist in Python 2.x, which seems to be your case
so don't try to import that in second line of your code
plus you made a typo (forgot semicolon) which works as a statement separator while writing inline function statements.
Similar to:
def img(url):
full='name'+'.jpeg'
urllib.urlretrieve(url,full)
I've got this issue where this piece of code:
import nltk
sentence = "Hello everyone on stackoverflow!"
tokens = nltk.word_tokenize(sentence)
print tokens
Throws me this kind of error, when ran from within PowerShell:
Traceback (most recent call last):
File "tokenize.py", line 1, in <module>
import nltk
File "C:\PYTHON27\lib\site-packages\nltk\__init__.py", line 105, in <module>
from collocations import *
File "C:\PYTHON27\lib\site-packages\nltk\collocations.py", line 37, in <module>
from nltk.util import ingrams
File "C:\PYTHON27\lib\site-packages\nltk\util.py", line 12, in <module>
import pydoc
File "C:\PYTHON27\LIB\pydoc.py", line 55, in <module>
import sys, imp, os, re, types, inspect, __builtin__, pkgutil, warnings
File "C:\PYTHON27\LIB\inspect.py", line 39, in <module>
import tokenize
File "C:\stuff\tokenize.py", line 4, in <module>
tokens = nltk.word_tokenize(sentence)
AttributeError: 'module' object has no attribute 'word_tokenize'
But the same code has no issues in IDLE:
>>> import nltk
>>> sentence = "Hello everyone on stackoverflow!"
>>> tokens = nltk.word_tokenize(sentence)
>>> print tokens
['Hello', 'everyone', 'on', 'stackoverflow', '!']
>>>
I can't seem to get it fixed no matter what I do. At first, I thought it's the " " space in the directory so I changed it to "stuff", reinstalled nltk but the problem still persisted.
I'd appreciate some help :)
If I correctly read you stack trace, you called your script tokenize.py. But tokenize is allready a module of the Standard Python Library. And still accordingly to the stack trace, the tokenize module is called indirectly from nltk. But as you have a tokenize.py in your current directory Python takes it instead of the standard one.
You should rename your script to a name that does not conflict with the Standard Python Library.
I added the following plugin to sublime text 2 which is meant to open the ipython qt console:
import sublime, sublime_plugin
from subprocess import call
# import os
class ipythonQtCommand(sublime_plugin.TextCommand):
def run(self, edit):
call(["start","ipython", "qtconsole", "--pylab", "--ConsoleWidget.font_size=9", "--ConsoleWidget.font_family='Consolas'"],shell=True)
Initially it worked just fine, i.e. the plugin opened an ipython shell. I then added a menu item and a key binding. At some point something must have gone wrong since now I get this error when I run the plugin:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Chris\Python27\lib\site-packages\IPython\__init__.py", line 43, in <
odule>
from .config.loader import Config
File "C:\Chris\Python27\lib\site-packages\IPython\config\__init__.py", line 1
, in <module>
from .application import *
File "C:\Chris\Python27\lib\site-packages\IPython\config\application.py", lin
31, in <module>
from IPython.config.configurable import SingletonConfigurable
File "C:\Chris\Python27\lib\site-packages\IPython\config\configurable.py", li
e 31, in <module>
from loader import Config
File "C:\Chris\Python27\lib\site-packages\IPython\config\loader.py", line 32,
in <module>
from IPython.utils.path import filefind, get_ipython_dir
File "C:\Chris\Python27\lib\site-packages\IPython\utils\path.py", line 29, in
<module>
from IPython.utils.process import system
File "C:\Chris\Python27\lib\site-packages\IPython\utils\process.py", line 25,
in <module>
from ._process_win32 import _find_cmd, system, getoutput, AvoidUNCPath, arg
split
File "C:\Chris\Python27\lib\site-packages\IPython\utils\_process_win32.py", l
ne 21, in <module>
import ctypes
File "C:\Chris\Python27\lib\ctypes\__init__.py", line 10, in <module>
from _ctypes import Union, Structure, Array
ImportError: Module use of python26.dll conflicts with this version of Python.
I tested the script by running it line by line in the st2 shell itself and it is the last line call([...]) that causes the error. I find this a little strange since the command works ok when calling it from somewhere else, e.g. ipython itself.
There seems to be a conflict with the st2 native python. In any case I managed to get os.system(...) to work when changing the working directory using os.chdir().
I have come across a strange python module import issue.
When I trying to import the boilerpipe module,
from boilerpipe.extract import Extractor
I got this exception:
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/boilerpipe-1.2.0-py2.7.egg/boilerpipe/extract/ __init__.py", line 2, in <module>
import urllib2
File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 1140, in <module>
import ssl
File "/usr/lib/python2.7/ssl.py", line 58, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 40, in <module>
class TextWrapper:
File "/usr/lib/python2.7/textwrap.py", line 82, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
I've searched over internet and saying that in Python 2.6 the 'str' module has been renamed to 'string' module. So this looks like some where in code library it didn't import "string" module properly.
Yet the really strange thing is, when I run the python code from home directory and run the same piece of code (either by using python shell or using python pyfile.py), it works fine! No more import error.
So I'm bit confusing. Can anyone give me any hint?
Thanks!
Some other script in sys.path is called "string.py" and is masking the stdlib module.
Double check to make sure that you don't have a file string.py that has been imported.
To debug this, put somewhere:
import sys
raise Exception("string module: %r" %(sys.modules.get("string"), ))
That will tell you what string module was imported (or if it shows None, no string module has been imported yet).