当前位置: 代码迷 >> python >> python exe文件上的ImportError
  详细解决方案

python exe文件上的ImportError

热度:86   发布时间:2023-06-16 14:14:43.0

使用py2exe,使用在找到的说明创建脚本的exe版本。 该脚本可以很好地编译并分别生成一个dist和build文件夹,但是当我在命令行上运行该程序时,会出现此错误。 注意,该脚本可以在我的IDE环境下正常运行,但是我打算给同事一个exe版本。如何解决此错误?

Traceback (most recent call last):
  File "tester2.py", line 4, in <module>
ImportError: No module named mechanize

这是setup.py文件:

from distutils.core import setup
import py2exe

setup(

    console = ['tester2.py'],
    zipfile = None,
)

您必须声明您的依赖项。 这是我的设置

setup(
    executables=executables,
    options=options,
    name='bla',
    version='0.3',
    packages=[...],
    url='',
    license='',
    author='',
    author_email='',
    description='', requires=['pandas', 'unidecode', 'click',
                              'xlsxwriter'] // you would have to add mechanize here
)

您是否已将文件添加到内部版本?

请查看setup.py中的include选项:

另外,这是我针对类似问题的解决方案,该解决方案是如何添加文件以构建并稍后运行: