PIL/枕头 - ValueError: py.__spec__ 未设置 [英] PIL/Pillow - ValueError: py.__spec__ is not set

查看:230
本文介绍了PIL/枕头 - ValueError: py.__spec__ 未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make an exe file using py2exe. Previously, this turned out well - I still have the working exe file of this code I edited. However, when I try running the below code now, I get a "ValueError: py.__spec__ is not set" error in Anaconda command prompt. Attempting to create exe file of another program comes out right.

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

Unfortunately, I saved over the old version of the program that worked earlier, but I reverted my new program to the old version from memory and it still doesn't work. Any ideas why this is happening? I believe it has to do with PIL module because that's the only difference between my other program that continues to get converted correctly to exe. Also, commenting out PIL module also leads to successful conversion. However, this means the program cannot be used for what it was made.

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'

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

推荐答案

我使用 python 3.8spyder 4.1.4.

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

When you are creating your exe with the library py2exe, sometimes there are problems with the import of modules. if you find the wrong module, you can exclude it in your setup.py.

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

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

The library py2exe use the script util.py, in the library importlib, to constructing importer. In this script, you add a print(module) in the function find_spec() in the else :

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

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

After add the print, when you will try to create an exe, in the console, you will see the imported modules. And when there is an error like :

ValueError: py.__spec__ is not set

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

Search the last module whose imported and exclude it in your setup.py. Module by module, you won't see any more mistakes.

  • 如何排除模块?:

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

To exclude a wrong module in your setup.py, write the name of the wrong module in the options. If you want, you can also exclude dll_excludes.

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

Below, i excluded wrong modules of your script :

from distutils.core import setup

import py2exe

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

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

I don't know if it's a good way to solves your problem, but it's functionnal with my scripts when i want to convert a py to an exe.

这篇关于PIL/枕头 - ValueError: py.__spec__ 未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆