1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Amazon Linux 2023でpythonコマンドが通らない時に確認すべきこと

Posted at

はじめに

Amazon Linuxのロードバランサーを作成時、動作確認としてpythonを使ったHTTPサーバの起動を試みた際に詰まったポイントがありました。

備忘録代わりとして起きた事象と対策を記載します。

前提

  • Amazonマシンイメージは『Amazon Linux 2023』である
  • 踏み台サーバとWebサーバは作成済みである
  • sshコマンドでWebサーバに接続できることを確認済みである
  • ロードバランサーを作成済みである

起きた事象

pythonとSimpleHTTPServerを用いてHTTPサーバを起動しようとしたところ、pythonがcommand not foundとなりました。

[ec2-user@ip-(WebサーバのプライベートIP) ~]$ python -m SimpleHTTPServer 3000
-bash: python: command not found¥

原因

原因1:Amazon Linux 2023はPython3系が導入されている

一つ目の原因は、Amazon Linux 2023ではpythonのバージョンがPython2系ではなくPython3系で動作することでした。

公式ドキュメント
https://docs.aws.amazon.com/ja_jp/linux/al2023/ug/python.html

AL2023 removed Python 2.7 and any components requiring Python are now written to work with Python 3.
(AL2023 では Python 2.7 が削除され、Python を必要とするすべてのコンポーネントは Python 3 で動作するように記述されるようになりました。)

Python3系ではpythonコマンドの代わりにpython3コマンドによって動作するようになります。
下記はpython3が入っていることの確認としてpython3のバージョンを見た例です。

[ec2-user@ip-(WebサーバのプライベートIP) ~]$ python3 --version
Python 3.9.25

原因2:python3ではSimpleHTTPServerモジュールではなくhttp.serverモジュールが使われる

Webサーバに接続するためのモジュールとしてSimpleHTTPServerを使うのはPython2系の記述方法であり、
Python3系ではhttp.serverモジュールを使うのが正しい記述でした。

まとめ

Amazon Linux 2023のAMIでHTTPサーバを起動したい場合、

python3 -m http.server [ポート番号]

という記述が正しい記述でした。

今回の気づきを機にpython3の他のコマンドも調べてみようと思います。

参考資料

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?