【问题标题】:AttributeError: 'module' object has no attribute 'fsdecode'; PYTHON 2.7; mac os XAttributeError:“模块”对象没有属性“fsdecode”;蟒蛇2.7; macOS X
【发布时间】:2018-08-26 16:31:56
【问题描述】:

我在 PYTHON 中找到了一个脚本。需要运行它来减少图像的大小。 为什么会发生此错误以及如何解决? (安装了 Python 2.7+ 为当前脚本安装了 PIL+ Imaging 1.1.7;Mac OS)

from __future__ import division
from __future__ import print_function
from __future__ import absolute_import

import argparse
import os
from PIL import Image

parser = argparse.ArgumentParser()
parser.add_argument('--image_dir', help='Directory of images to resize')
args = parser.parse_args()

image_dir = os.getcwd() + "/" + args.image_dir

for f in os.listdir(image_dir):
    filename = os.fsdecode(f)
    image = Image.open(image_dir + '/' + filename)
    print(image_dir + '/' + filename)
    height, width = image.size
    if width > 1000:
        resize_amt = 1000 / width
        new_height = int(round(height * resize_amt))
        image = image.resize((new_height, 1000))
        image.save(os.getcwd() + "/" + image_dir + "/" + filename)

在我解决了所有问题后,此文本出现在终端中:

Oleksandrs-MacBook-Air: jaskier$ python resize.py --image_dir=/Images/

Traceback(最近一次调用最后一次):

文件“resize.py”,第 16 行,在 文件名 = os.fsdecode(f)

AttributeError: 'module' 对象没有属性 'fsdecode'

【问题讨论】:

  • 说'os'模块没有'fsdecode'函数
  • 这可能意味着您使用的 python 版本在 os 模块中没有 fsdecode ...
  • python --version Python 2.7.10 已经尝试安装3.6,但是系统PATH被2.7覆盖了-_-
  • 嗯,试试 doin python3 —version
  • *已从 python.org/downloads 手动安装 3.6.4 结果:上次登录:3 月 24 日星期六 14:25:50 在 ttys000 Oleksandrs-MacBook-Air:~ jaskier$ python --version Python 2.7。 10

标签: python-2.7 terminal macos-high-sierra


【解决方案1】:

Python 2.7(Mac OS 原生)和 Python 3+(应使用终端安装或https://www.python.org/download/releases/3.0/)之间存在 Hugh 差异

终端:

pip3 install Pillow

这将有助于在 PIL 和 Python3 之间建立依赖关系......(此外检查文件夹中的 Python3:

/Library/Frameworks/Python.framework/Versions/3.6/bin/ 如果您已正确完成所有操作,pip3 也应该存在。

执行此步骤后,现在我遇到了一些代码错误,但没有任何模块错误。我希望它会对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多