gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:33:10 25 4
gpt4 key购买 nike

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

最佳答案

我使用 python 3.8spyder 4.1.4

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

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

库py2exe使用库importlib中的脚本util.py来构建importer。在此脚本中,您在 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 时,它​​对我的​​脚本起作用。

关于anaconda - PIL/Pillow - ValueError : py. __spec__ 未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67135887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com