我在下面做了一个简单的脚本来说文本:

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)


我在外部文件中运行该函数,然后将其内置到cx_Freeze的setup.py中。

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
      )


上面的代码进行设置。
在执行exe文件之后:

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


在cmd中我得到这个错误:

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'


谁能解释为什么我会收到此错误?

最佳答案

更新:我发现手动将依赖项复制到内部版本的lib文件夹解决了该问题,因为它仅复制了idna模块的一半。

关于python - Python cx_Freeze ImportError:无法导入名称“idnadata”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47911544/

10-12 21:37