Python cx_freeze导入错误

时间:2017-12-20 17:37:30

标签: python python-3.x cmd importerror cx-freeze

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

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)

我在外部文件中运行该函数,然后将其构建到setup.py中以进行cx冻结。

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之后: 建立
+ -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'

任何人都可以解释我收到此错误的原因吗?

2 个答案:

答案 0 :(得分:2)

更新:我发现手动将依赖项复制到构建的lib文件夹中解决了问题,因为它只复制了一半的idna模块。

答案 1 :(得分:0)

FWIW,我今天在RHEL 7.5上使用cx_Freeze 5.1.1和Python 3.6.0(ActivePython)遇到了这个问题。在setup.py中尝试了许多排列并失败之后,我通过以下方式运行cxfreeze脚本解决了该问题:

cxfreeze myscript.py --include-modules=requests,requests.packages.idna,requests.packages.idna.idnadata

我发现在setup.py文件中声明的其他软件包和模块也被忽略了,这种将它们添加到--include-modules列表中的技术对它们也有效。

正如我所说的,FWIW。