- Python
‘NoneType’ object has no attributeの直し方
AttributeError: 'NoneType' object has no attribute 'group' は、None(値が無いことを表す特別な値)に対して、.group() のようなメソッドや属性を使おうとしたときのエラーです。 原因は「None に .group を付けたこと」ではなく、その手前で、値が None になっていたことです。
エラー文の最後の 'group' の直前にある変数・式が None です。 次の表の、どれに当たるかを確かめます。
| None が返る代表例 | エラー文の例 | 直し方 |
|---|---|---|
re.match が一致しない |
no attribute 'group' |
if m: で確かめる。先頭以外なら re.search |
BeautifulSoup の find が見つからない |
no attribute 'text' |
if tag: で確かめる。タグ名・class を見直す |
list.sort() や append() の戻り値を代入した |
no attribute 'append' |
代入しない(xs.sort() だけ書く) |
dict.get() でキーが無い |
no attribute 'lower' |
既定値を渡す(d.get("email", "")) |
os.environ.get() で環境変数が無い |
no attribute 'split' |
既定値を渡す、または設定を確かめる |
関数の return 忘れ |
no attribute 'upper' |
すべての分岐で return する |
print() の戻り値を使った |
no attribute 'strip' |
print は None を返す |
確認環境:Python 3.10・3.14、BeautifulSoup 4.15.0 で、7つの例を再現しました(確認日 2026年10月11日)。
本記事の内容は、ご自由にお使いください。
ご利用の際は、出典として本ページへのリンクを記載いただけますようお願いします。
(記載例)出典:株式会社RJC「'NoneType' object has no attributeの直し方」
例① re.match が一致しない
import re
m = re.match(r"(\d{3})-(\d{4})", "〒100-0001")
print(m.group(1))
Traceback (most recent call last):
File "/srv/s4/p9/n2.py", line 3, in <module>
print(m.group(1))
^^^^^^^
AttributeError: 'NoneType' object has no attribute 'group'
re.match は、文字列の先頭から一致するかを調べます。 〒100-0001 は先頭が 〒 なので一致せず、None が返りました。公式ドキュメントでも、一致しないときは None を返すと説明されています。
| 関数 | 調べる場所 | "〒100-0001" の結果 |
|---|---|---|
re.match |
先頭だけ | None |
re.search |
文字列のどこか | 一致(100) |
m = re.search(r"(\d{3})-(\d{4})", text)
if m is None:
print("郵便番号が見つかりません")
else:
print(m.group(1))
一致しないこともある入力では、必ず None かどうかを確かめてから .group() を使います。 Python 3.8 以降なら、if (m := re.search(...)): と1行で書けます。なお、公式ドキュメントによると、Python 3.15 では re.match と同じ動きの re.prefixmatch が加わっています。
例② BeautifulSoup の find が見つからない
from bs4 import BeautifulSoup
soup = BeautifulSoup('<div class="price">1,200円</div>', "html.parser")
soup.find("div", class_="price").text # '1,200円'
soup.find("span", class_="price").text # AttributeError: 'NoneType' object has no attribute 'text'
find は、見つからないと None を返します(select_one も同じく None でした)。スクレイピングでは、サイトの HTML が変わった・JavaScript で後から表示される・ページによってタグが無いなどで、ある日突然このエラーになります。
tag = soup.find("span", class_="price")
price = tag.text if tag else "" # 見つからないときの値を決めておく
BeautifulSoup の使い方は、既存の記事「Python BeautifulSoupスクレイピング|コピペ用コードとメソッド一覧」で扱っています。
例③ sort・append の戻り値を代入した
xs = [3, 1, 2]
xs = xs.sort() # ← xs が None になる
xs.append(4) # AttributeError: 'NoneType' object has no attribute 'append'
list.sort()・append()・extend()・reverse() などは、リストをその場で変更し、None を返します。 公式ドキュメントでも、sort() は「並べ替えたリストを返さない(新しいリストが欲しいなら sorted() を使う)」と説明されています。
| 書き方 | 結果 |
|---|---|
xs.sort() |
xs が並べ替わる(戻り値は None) |
xs = xs.sort() |
xs が None になる |
ys = sorted(xs) |
並べ替えた新しいリスト |
例④ dict.get・os.environ.get でキーが無い
d = {"name": "a"}
d.get("email").lower() # AttributeError: 'NoneType' object has no attribute 'lower'
d.get("email", "").lower() # 既定値を渡す → ''
get は、キーが無いと None を返します(d["email"] ならKeyError)。環境変数の os.environ.get("NAME") も同じで、設定し忘れると None になり、.split(",") などでこのエラーになりました。必須の設定は、None のときに分かりやすいメッセージで止めるようにしておきます。
import os
hosts = os.environ.get("ALLOWED_HOSTS")
if hosts is None:
raise RuntimeError("環境変数 ALLOWED_HOSTS が設定されていません")
例⑤ 関数の return 忘れ
def label(x):
if x > 0:
return "pos"
# x <= 0 のとき、return が無い
label(-1).upper() # AttributeError: 'NoneType' object has no attribute 'upper'
関数が return に届かずに終わると、None を返します。 条件によって、ある値のときだけエラーになるので、テストで見つけにくい形です。辞書を返す関数なら、同じ原因で TypeError: 'NoneType' object is not subscriptable になります(関連記事の「NoneType object is not subscriptableの直し方」で扱っています)。
どこで None になったかを突き止める手順
式がつながっているときは、エラー文の属性名から、どこが None かを読みます。
data = {"user": {"name": None}}
print(data.get("user").get("name").lower())
print(data.get("user").get("name").lower())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'lower'
'lower' の手前、つまり data.get("user").get("name") が None です。Python 3.11 以降は、^^^ の印で問題の式が示されました(3.10 では印は出ませんでした)。
| 手順 | 方法 |
|---|---|
| 1. エラー文の属性名を見る | no attribute 'lower' → .lower() の手前が None |
| 2. 手前の式を分けて print する | u = data.get("user"); print(repr(u)) |
| 3. None を返した関数を調べる | 上の表(match・find・get・return 忘れ) |
| 4. None になりうるなら、確かめる処理を入れる | if x is None:、既定値 |
よくある質問
Q. try/except で AttributeError を握りつぶせばよいですか。
A. ほかの原因の AttributeError(属性名の打ち間違いなど)も隠れてしまいます。None になりうる場所で if x is None: を書くほうが、原因が分かりやすく残ります。
まとめ
- None に
.○○を付けたエラー。原因は手前で None になったこと re.matchは先頭だけを調べ、一致しないと None- BeautifulSoup の
findは見つからないと None xs = xs.sort()は xs が None になる。sorted()を使うgetは既定値を渡す、関数はすべての分岐で return
参考・出典
確認日はいずれも 2026年10月11日です。
- Python ドキュメント「re」(match・search の戻り値):https://docs.python.org/3/library/re.html
- Python ドキュメント「Built-in Types」(list.sort):https://docs.python.org/3/library/stdtypes.html
本記事の内容は、ご自由にお使いください。
ご利用の際は、出典として本ページへのリンクを記載いただけますようお願いします。
(記載例)出典:株式会社RJC「'NoneType' object has no attributeの直し方」
株式会社RJC ― SI事業・SES事業・AI駆動開発。RJCは一緒に成長を楽しめる会社です。
WE ARE HIRING
RJCで一緒に開発しながら、
成長を楽しみませんか?
RJCは、Web・モバイル・AIを活用した開発プロジェクトで、テックリードやPM・PMOも活躍するシステム開発会社です。会社を知る、待遇を確かめる、話を聞いてみる。気になるところ見てみてください!
- 127日年間休日
- 12時間平均残業時間
- 毎日ガチャ遊びココロも大切にする福利厚生。アマギフなどの賞品ラインナップ!
ほかにも、チケットレストラン、書籍読み放題、2年ごとの慰労報奨(休暇 or 金一封)、11期連続の黒字決算。