3
2

More than 3 years have passed since last update.

【Python】module 'datetime' has no attribute 'date'のエラーにハマった時のこと。

Last updated at Posted at 2020-08-03

初めてdatetimeモジュールを使った時に、
想定外のエラーでハマったので共有します。

実行したかった事

ただ、今日の日付を表示したかった。。。

import datetime

today = datetime.date.today()
print(today.strftime('%Y%m%d'))

これで表示できるはずなのに、
なぜか「module 'datetime' has no attribute 'date'」とエラーになってしまう。。

文法が違うのか、スペルミスか、
いやいやコピペしているんだからそんなわけない・・・

1時間ほどの時間を費やし、やっと原因判明しました。

原因

原因は、ファイル名を"datetime.py"としていたことでした。
Pythonではファイル名をモジュールと同じ名前にすると、
うまく実行できなくなるようです。

3
2
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
3
2