LoginSignup
0
0

More than 3 years have passed since last update.

PythonエラーAttributeError: module 'xxx' has no attribute 'yyy'。こんなケースも。。。

Posted at

目的

AttributeError: module 'xxx' has no attribute 'yyy'
というエラーメッセージに関しては、いろいろな解説がある。
どこかの記事とは重複すると思うが、以下の観点で、
このエラーメッセージのことを記載する。

 ・attributeそもそもの定義にかかわる内容
   →そのため、「has no」と言われたり言われなかったり、
         呼び方により変わるケース

記載の目的は、エラーメッセージを読み間違うと、対策に無駄な時間を要する場合があるので、それの回避のための情報共有。

attributeなのか、attributeじゃないのか?

attributeの定義

(出典:
 https://docs.python.org/3/glossary.html
 https://docs.python.org/ja/3/glossary.html

attribute
(属性) オブジェクトに関連付けられ、ドット表記式によって名前で参照される値です。例えば、オブジェクト o が属性 a を持っているとき、その属性は o.a で参照されます。
A value associated with an object which is referenced by name using dotted expressions. For example, if an object o has an attribute a it would be referenced as o.a.

エラーが出たり出なかったりするケース

上記のattributeの定義は、広義だと思う。ドットでつながれば、、、という定義。
ご存じのように、パッケージとかも、ドットでつながる。

さて、

ちょっと、古いモジュールを例にします。

>>> import scipy.misc
>>> scipy.__version__
'1.1.0'

pilutilは、 'scipy.misc' のattributeなんでしょうか?違うのでしょうか?

>>>
>>> help(scipy.misc.pilutil)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'scipy.misc' has no attribute 'pilutil'
>>>
>>>
>>> import scipy.misc.pilutil as tmppil
>>> help(tmppil)
Help on module scipy.misc.pilutil in scipy.misc:

NAME
    scipy.misc.pilutil - A collection of image utilities using the Python Imaging Library (PIL).

DESCRIPTION
    Note that PIL is not a dependency of SciPy and this module is not
    available on systems that don't have PIL installed.

FUNCTIONS
    bytescale(*args, **kwds)
        `bytescale` is deprecated!
        `bytescale` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.


            Byte scales an array (image).

            Byte scaling means converting the input image to uint8 dtype and scaling
            the range to ``(low, high)`` (default 0-255).
            If the input image already has dtype uint8, no scaling is done.

            This function is only available if Python Imaging Library (PIL) is installed.

            Parameters

>>>


:pencil:パッケージとかで、違う(AttributeError)と言われても、必ずしも、そうじゃない
んじゃないかな?と思います。

ここでは、attributeとして扱えません、ぐらいの意味で解釈したほうがいいときもあるのでは?
importはタフに処理してくれるので、そこで、処理して(仲介して)使えばいい気がしています。

まとめ

Pythonのエラーメッセージはわかり易いと思っており、
ちょっと、変なのがあると、少し、傾向をつかんでおきたいので、
やや細かいことをケアしました。

関連(本人)

pythonをストレスなく使う!(結局docs.python.orgのreferenceが納得感が高い気が、、、)
pythonをストレスなく使う!(generatorに詳しくなる。since1975らしい。)
pythonをストレスなく使う!(Pythonでは、すべてがオブジェクトとして実装されている)
pythonをストレスなく使う!(Pylintに寄り添う)
pythonをストレスなく使う!(ExpressionとStatement)
英語と日本語、両方使ってPythonを丁寧に学ぶ。

今後

コメントなどあれば、お願いします。:candy:
勉強します、、、、

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0