如何阻止在py2exe中编译的Python程序显示导入错误:没有模块名称"ctypes"



我想知道这是否可能是一个编译错误,或者我可以做些什么来阻止它显示。我已经为cmd做了一个argparse程序。我用py2exe编译它,当我运行它时,它正确地精确程序,但在运行代码之前总是给出这个错误:

Traceback (most recent call last):
  File "boot_common.py", line 46, in <module>
ImportError: No module named 'ctypes'

如果它是在我的代码,这里是我的脚本:

import argparse
import zipfile
import os
from contextlib import closing
def parse_args():
    parser = argparse.ArgumentParser('ziputil '+
    '-m <mode> -f <file> -p <output>')
    parser.add_argument('-f', action="store", dest='files', type=str,
                        help='-f <file> : Specify the files to be zipped, or the .zip to be unzipped.')
    parser.add_argument('-m', action="store", dest='mode', type=str,
                        help='-m <mode> : Zip to zip files, UnZip, to unzip files, or     ZipDir to zip entire directories.')
    parser.add_argument('-p', action="store", dest='path', type=str, nargs='?',     const=os.getcwd(),
                        help='-p <path> : specify the path to unpack/pack to.')

    return vars(parser.parse_args())
def unzipPackage(path, files):
    with zipfile.ZipFile(files, "r") as z:
        z.extractall(path)
def zipPackage(path, files):
    files = files.split(', ')
    zf = zipfile.ZipFile(path, mode='w')
    try:
        for file in files:
            zf.write(file)
    finally:
        zf.close()
def zipdir(path, zip):
    for root, dirs, files in os.walk(path):
        for file in files:
            zip.write(os.path.join(root, file))

dict = parse_args()
files = dict['files']
path = dict['path']
mode = dict['mode']
if mode == 'Zip':
    zipPackage(path, files)
elif mode == 'UnZip':
    unzipPackage(path, files)
elif mode == 'ZipDir':
    zipf = zipfile.ZipFile(path, 'w')
    zipdir(files, zipf)
    zipf.close()

这是由py2exe中的错误引起的,它将在下一个版本中修复。更多信息

解决方案是在C:Python34Libsite-packagespy2exeruntime.py文件(第117行)中将ctypes添加到bootstrap_modules

...
# modules which are always needed
bootstrap_modules = {
    # Needed for Python itself:
    "ctypes",
    "codecs",
    "io",
    "encodings.*",
    }
...

相关内容

  • 没有找到相关文章

最新更新