Search code examples
pythonpython-3.xpython-2.7importgtts

"from gtts import gTTS" works fine in python 2.7 but it does not work in python 3.5. Why?


I want to execute this code:

from gtts import gTTS
tts = gTTS('hello', lang='en')
tts.save('hello.mp3')

In python (python 2.7.13) works fine but in python3 (python 3.5.3) is not working.

It has always worked in the old python. Now, on a new PC (a raspberry) I wanted to start using the python3 (3.5.3) so I tried and it didn't work.

As it was a new installation, the gtts maybe was not installed, so I installed with:

pi@raspberrypi:~ $ pip install gTTS

I had this:

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 $ 

I tried again and it was not working. I tried with the old 2.7 version and for my surprise, it was working.

Working in 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
>>> 

Not working in 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'
>>> 

Then, I noticed that there is pip3 ! When I do

pi@raspberrypi:~ $ sudo pip3 install gTTS

gTTS is not there, so I did

pi@raspberrypi:~ $ sudo pip3 install gTTS

But after doing that i get red text and error msg

 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'

When I check again with pip list, gTTS is still not there... Any ideas? Thanks


Solution

  • You are complaining that you ran a certain intpreter and saw:

    from gtts import gTTS
    ...
    ImportError: No module named 'gtts'
    

    or more succinctly:

    import gtts
    ...
    ImportError: No module named 'gtts'
    

    The difficulty is that you never installed gtts for that interpreter.

    Yes, you ran pip (or equivalently, pip2), which offers terrific service for your python2.7 interpreter. No, you never ran pip3, which might work well for your python3.5 interpreter.

    It would probably be better to invoke it in this way:

    $ python3 -m pip install gTTS
    

    Then you're quite sure what sys.path will be during installation, and that the package will land in the right place for import to find it.

    As a separate item, 3.5 is a bit old, consider using a newer version.