Search code examples
pythonpython-3.xcmdimporterrorcx-freeze

Python cx_Freeze ImportError: cannot import name 'idnadata'


I have made a simple script to say text, below:

from gtts import gTTS
import os
import random
from playsound import playsound

def say(voice_text, capitalize = True):

    voice = gTTS(text = (voice_text), lang = "en")
    fileName = "%d.mp3" %random.randint(1, 999999999)
    voice.save(fileName)
    if capitalize == True:
        print(voice_text.capitalize()+"...")
    else:
        print(voice_text+"...")

    playsound(fileName)

    os.remove(fileName)

I run the function in an external file and then build that into the setup.py for cx_Freeze.

from cx_Freeze import setup, Executable

executables = [
    Executable('main.py',
               shortcutName="test",
               shortcutDir="DesktopFolder",
               icon="icon.ico")
]

setup(name='hello',
      version='0.1',
      description='Sample cx_Freeze script',
      executables=executables
      )

Above code for setup. After executing the exe under:

build
+-exe.win-amd64-3.6
| -main.exe

in cmd I get this error:

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\Python36\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "main.py", line 1, in <module>
  File "C:\Users\name\Desktop\pyExe test\other.py", line 2, in <module>
    from say import say
  File "C:\Users\name\Desktop\pyExe test\say.py", line 1, in <module>
    from gtts import gTTS
  File "C:\Python36\lib\site-packages\gtts\__init__.py", line 2, in <module>
    from .tts import gTTS
  File "C:\Python36\lib\site-packages\gtts\tts.py", line 2, in <module>
    import re, requests, warnings
  File "C:\Python36\lib\site-packages\requests\__init__.py", line 98, in <module>
    from . import packages
  File "C:\Python36\lib\site-packages\requests\packages.py", line 7, in <module>
    locals()[package] = __import__(package)
  File "C:\Python36\lib\site-packages\idna\__init__.py", line 2, in <module>
    from .core import *
  File "C:\Python36\lib\site-packages\idna\core.py", line 1, in <module>
    from . import idnadata
ImportError: cannot import name 'idnadata'

Can anyone explain why I am getting this error?


Solution

  • Update: I have found that manually copying the dependencies into the lib folder of the build fixed the problem as it was only copying half of the idna module.