我想执行这段代码:
from gtts import gTTS
tts = gTTS('hello', lang='en')
tts.save('hello.mp3')
在 python (python 2.7.13) 中工作正常,但在 python3 (python 3.5.3) 中不起作用。
它一直在旧 python 中工作。现在,在一台新PC(树莓)上,我想开始使用python3(3.5.3),所以我尝试了,但没有成功。
由于是新安装,可能没有安装 gtts,所以我安装了:
pi@raspberrypi:~ $ pip install gTTS
我有这个:
Collecting gTTS
Downloading
...
...
...
Successfully built gTTS bs4 gtts-token
Installing collected packages: backports.functools-lru-cache, soupsieve, beautifulsoup4, bs4, click, idna, chardet, certifi, urllib3, requests, gtts-token, six, gTTS
Successfully installed backports.functools-lru-cache-1.5 beautifulsoup4-4.7.1 bs4-0.0.1 certifi-2019.3.9 chardet-3.0.4 click-7.0 gTTS-2.0.3 gtts-token-1.1.3 idna-2.8 requests-2.22.0 six-1.12.0 soupsieve-1.9.1 urllib3-1.25.2
pi@raspberrypi:/home $
我又试了一次,还是不行。我尝试使用旧的 2.7 版本,令我惊讶的是,它可以正常工作。
在python中工作:
pi@raspberrypi:/ $ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from gtts import gTTS
>>>
不能在 python3 中工作
pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gtts import gTTS
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gtts'
>>>
然后,我注意到有 pip3 !当我做
pi@raspberrypi:~ $ sudo pip3 install gTTS
gTTS 不存在,所以我做了
pi@raspberrypi:~ $ sudo pip3 install gTTS
但是在这样做之后我得到红色文本和错误消息
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 643, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/retry.py", line 315, in increment
total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
当我再次检查 pip list 时,gTTS 仍然不存在......有什么想法吗?谢谢