PIL/Pillow - ValueError: py.__spec__ 未设置

Posted

技术标签:

【中文标题】PIL/Pillow - ValueError: py.__spec__ 未设置【英文标题】:PIL/Pillow - ValueError: py.__spec__ is not set 【发布时间】:2021-07-12 02:37:12 【问题描述】:

我正在尝试使用 py2exe 创建一个 exe 文件。以前,结果很好 - 我仍然有我编辑的这段代码的工作 exe 文件。但是,当我现在尝试运行以下代码时,我在 Anaconda 命令提示符中收到“ValueError:py.__spec__ 未设置”错误。尝试创建另一个程序的exe文件是正确的。

不幸的是,我保存了之前运行的旧版本程序,但我将新程序从内存中恢复为旧版本,但它仍然无法运行。任何想法为什么会发生这种情况?我相信它与 PIL 模块有关,因为这是我的其他程序继续正确转换为 exe 之间的唯一区别。此外,注释掉 PIL 模块也会导致转换成功。但是,这意味着该程序不能用于它的制作。

py2exe setup.py 代码:

from distutils.core import setup

import py2exe

setup(console=['resizeImg.py'])

我的程序:

from PIL import Image
import os, sys
from pathlib import Path

while True:
    # Warning message for users
    print('')
    print('IMPORTANT!!!'.center(len('IMPORTANT!!!')+12,'*'))
    print('If a file exists in the same name in destination folder, \
it will be IRREVERSIBLY OVERWRITTEN!')
    print('*'*(len('IMPORTANT!!!')+12))
    
    print('\nEnter full paths for source and destination. \
          \nEnter q to quit.')
          
    spath = input('Source path: ')
    if spath == 'q':
        sys.exit()
    dpath = input('Destination path: ')
    if dpath == 'q':
        sys.exit()
    elif dpath == spath:
        Path(spath,'scaledImages').mkdir(exist_ok=True)
        dpath = str(Path(spath,'scaledImages'))
    htsize = int(input('Height dimension: '))
    if htsize == 'q':
        sys.exit()
    
    os.chdir(Path(spath))
    optSize = htsize
    
    for filename in os.listdir('.'):
        print (filename)
        if not (filename.endswith('.jpeg') or filename.endswith('.jpg') or filename.endswith('.png') or filename.endswith('.bmp')):
            continue
        else:
            im = Image.open(filename)
            width, height = im.size
            
        if height > optSize:
            # commented out to remove width height check
            # if width > height:
            #     height = int((optSize / width) * height)
            #     width = optSize
                
            # else:
            #     width = int((optSize / height) * width)
            #     height = optSize
            width = int((optSize / height) * width)
            height = optSize
            
                
            print('')
            print(f'Resizing filename ...')
            
            im = im.resize((width,height))
            
            im.save(Path(dpath) / filename)
            
        else:
            im.save(Path(dpath) / filename)
            
        print('New dimension: ' + str(width) + ' x ' + str(height))
            
    print('\nRescaling complete. Rescaled images saved in: ' + \
          dpath + '\n')
    quitPrompt = input('q to quit; any other input for new search: ')
    if quitPrompt.lower() == 'q':
        sys.exit()

当我尝试通过运行 'python setup.py py2exe'

在 Anaconda 命令提示符下执行 py2exe
running py2exe
Traceback (most recent call last):
  File "setup.py", line 11, in <module>
    setup(console=['resizeImg.py'])
  File "C:\Users\hamis\anaconda3\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Users\hamis\anaconda3\lib\distutils\dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "C:\Users\hamis\anaconda3\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\distutils_buildexe.py", line 192, in run
    self._run()
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\distutils_buildexe.py", line 272, in _run
    builder.analyze()
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\runtime.py", line 177, in analyze
    target.analyze(mf)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\runtime.py", line 78, in analyze
    modulefinder.run_script(self.script)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 62, in run_script
    self._scan_code(mod.__code__, mod)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 386, in _scan_code
    self.safe_import_hook(name, mod, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 122, in safe_import_hook
    self.import_hook(name, caller, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 109, in import_hook
    self._handle_fromlist(module, fromlist, caller)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 180, in _handle_fromlist
    self._gcd_import('.'.format(mod.__name__, x))
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 258, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 348, in _find_and_load
    self._scan_code(module.__code__, module)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 393, in _scan_code
    self._scan_code(c, mod)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 393, in _scan_code
    self._scan_code(c, mod)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 386, in _scan_code
    self.safe_import_hook(name, mod, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 122, in safe_import_hook
    self.import_hook(name, caller, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 109, in import_hook
    self._handle_fromlist(module, fromlist, caller)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 180, in _handle_fromlist
    self._gcd_import('.'.format(mod.__name__, x))
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 258, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 348, in _find_and_load
    self._scan_code(module.__code__, module)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 393, in _scan_code
    self._scan_code(c, mod)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 393, in _scan_code
    self._scan_code(c, mod)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 386, in _scan_code
    self.safe_import_hook(name, mod, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 122, in safe_import_hook
    self.import_hook(name, caller, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 104, in import_hook
    module = self._gcd_import(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 258, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 348, in _find_and_load
    self._scan_code(module.__code__, module)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 386, in _scan_code
    self.safe_import_hook(name, mod, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 122, in safe_import_hook
    self.import_hook(name, caller, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 109, in import_hook
    self._handle_fromlist(module, fromlist, caller)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 180, in _handle_fromlist
    self._gcd_import('.'.format(mod.__name__, x))
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 258, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 348, in _find_and_load
    self._scan_code(module.__code__, module)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 386, in _scan_code
    self.safe_import_hook(name, mod, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 122, in safe_import_hook
    self.import_hook(name, caller, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 104, in import_hook
    module = self._gcd_import(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 258, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 348, in _find_and_load
    self._scan_code(module.__code__, module)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 393, in _scan_code
    self._scan_code(c, mod)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 393, in _scan_code
    self._scan_code(c, mod)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 386, in _scan_code
    self.safe_import_hook(name, mod, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 122, in safe_import_hook
    self.import_hook(name, caller, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 104, in import_hook
    module = self._gcd_import(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 258, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 348, in _find_and_load
    self._scan_code(module.__code__, module)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 386, in _scan_code
    self.safe_import_hook(name, mod, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 122, in safe_import_hook
    self.import_hook(name, caller, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 104, in import_hook
    module = self._gcd_import(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 258, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 348, in _find_and_load
    self._scan_code(module.__code__, module)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 386, in _scan_code
    self.safe_import_hook(name, mod, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 122, in safe_import_hook
    self.import_hook(name, caller, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 109, in import_hook
    self._handle_fromlist(module, fromlist, caller)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 180, in _handle_fromlist
    self._gcd_import('.'.format(mod.__name__, x))
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 258, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 348, in _find_and_load
    self._scan_code(module.__code__, module)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 386, in _scan_code
    self.safe_import_hook(name, mod, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 122, in safe_import_hook
    self.import_hook(name, caller, fromlist, level)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 104, in import_hook
    module = self._gcd_import(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 258, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\hamis\anaconda3\lib\site-packages\py2exe\mf34.py", line 303, in _find_and_load
    spec = importlib.util.find_spec(name, path)
  File "C:\Users\hamis\anaconda3\lib\importlib\util.py", line 111, in find_spec
    raise ValueError('.__spec__ is not set'.format(name)) from None
ValueError: py.__spec__ is not set

【问题讨论】:

【参考方案1】:

我使用 python 3.8spyder 4.1.4

当您使用库 py2exe 创建 exe 时,有时导入模块会出现问题。如果您发现错误的模块,您可以在 setup.py 中排除它。

创建 exe 时如何在导入过程中找到错误的模块? :

py2exe 库使用库 importlib 中的脚本 util.py 来构建导入器。在此脚本中,您在 else 的函数 find_spec() 中添加一个 print(module) :

else:
    module = sys.modules[fullname]
    print(module)
    if module is None:
        return None

添加打印后,当您尝试创建 exe 时,在控制台中,您将看到导入的模块。 当出现如下错误时:

ValueError: py.__spec__ is not set

在 setup.py 中搜索最后一个导入并排除它的模块。 逐个模块,你不会再看到任何错误。

如何排除模块? :

要在 setup.py 中排除错误模块,请在选项中写入错误模块的名称。如果需要,也可以排除 dll_excludes。

下面,我排除了您脚本的错误模块:

from distutils.core import setup

import py2exe

setup(options = "py2exe":   "excludes": ["_pytest","qtpy"]  ,
      console = ['resizeImg.py'])

我不知道这是否是解决您问题的好方法,但是当我想将 py 转换为 exe 时,它​​对我的​​脚本很有用。

【讨论】:

非常感谢,pont_!我还没有完全实现并尝试过这个编辑。但它的信息量非常大,而且不仅仅针对这个问题。让我更好地了解了这些库的工作原理。【参考方案2】:

p2exe 在 python 版本 3.6 之前一直有效

如果您设法降级 python 运行时,它应该编译良好。否则,您将需要使用 pyinstaller 等替代库

【讨论】:

谢谢,希瓦吉。这可能是我记得当时升级 Python 的原因。我已经开始使用 pyinstaller,它似乎对我很有效。

以上是关于PIL/Pillow - ValueError: py.__spec__ 未设置的主要内容,如果未能解决你的问题,请参考以下文章

python pil 怎么安装

(PIL / Pillow)尽管坐标正确,但文本位置错误

如何为 Python 3.6 安装 PIL/Pillow?

提交带有表格的图像时需要Django Pillow或Pil吗?

如何使用 PIL/Pillow 将图像合并到画布中?

PIL(Pillow)的学习笔记