【发布时间】:2019-12-22 14:08:00
【问题描述】:
在 raspbian 中使用 virtualenv (16.7.7) 在 pipenv (2018.11.26) 中运行下面的代码片段时,它可以完美执行并且所有操作都按预期完成。
import logging
import distutils
from PIL import Image, ImageDraw, ImageFont, ImageFile, ImageOps
from pathlib import Path
logger = logging.getLogger(__name__)
logger.root.setLevel('DEBUG')
image = Path('/home/pi/tmp/4886.jpg').expanduser()
size = (100, 200)
try:
logging.info(f'opening image: {image}')
im = Image.open(image)
im.thumbnail(size)
except (PermissionError, FileNotFoundError, OSError) as e:
logging.warning(f'could not open image file: {image}')
logging.warning(f'image error: {e}')
logging.warning(f'using empty image')
print(f'image size is: {im.size}')
输出 这会产生预期的结果
INFO:root:opening image: /home/pi/tmp/4886.jpg
image size is: (100, 90)
用pipenv run python -m PyInstaller im_open.py打包后编译版本报错DEBUG:PIL.Image:Image: failed to import JpegImagePlugin: No module named 'distutils'
输出
INFO:root:opening image: /home/pi/tmp/4886.jpg
...
DEBUG:PIL.Image:Importing IptcImagePlugin
DEBUG:PIL.Image:Importing JpegImagePlugin
DEBUG:PIL.Image:Image: failed to import JpegImagePlugin: No module named 'distutils'
DEBUG:PIL.Image:Importing Jpeg2KImagePlugin
DEBUG:PIL.Image:Importing McIdasImagePlugin
DEBUG:PIL.Image:Importing MicImagePlugin
DEBUG:PIL.Image:Image: failed to import MicImagePlugin: No module named 'olefile'
DEBUG:PIL.Image:Importing MpegImagePlugin
DEBUG:PIL.Image:Importing MpoImagePlugin
DEBUG:PIL.Image:Image: failed to import MpoImagePlugin: No module named 'distutils'
DEBUG:PIL.Image:Importing MspImagePlugin
DEBUG:PIL.Image:Importing PalmImagePlugin
DEBUG:PIL.Image:Importing PcdImagePlugin
...
WARNING:root:could not open image file: /home/pi/tmp/4886.jpg
WARNING:root:image error: cannot identify image file '/home/pi/tmp/4886.jpg'
WARNING:root:using empty image
Traceback (most recent call last):
File "im_open.py", line 69, in <module>
print(f'image size is: {im.size}')
NameError: name 'im' is not defined
我尝试了以下方法:
- 明确包括 distutils(如上面的代码片段所示)
- 将 distutils 添加到 .spec 文件中的 hiddenimports 列表中:
hiddeimports=['distutils']
相关研究
- 有一个 closed bug relating to virtualenv 16.4.0 与此相关,但我不确定它会如何影响我的问题
【问题讨论】:
标签: python virtualenv pyinstaller pipenv