0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonバージョン出力のプチ迷走

Posted at

要約

pythonのバージョンによって、「python -V」コマンド実行による出力はstdoutで出力する場合とstderrで出力する場合に別れるよ....

詳細

Pythonのバージョンを確認する際、下記のようなコマンドを打つでしょう。

python -V
python2 -V
python3 -V

コマンドラインで表示されるバージョンをみんなは読み取るわけで。
ただ、この出力をShellゲーで取ろうとした時、プチ罠にハマった....。
Pythonはバージョンによってこのバージョン出力の方式が違うのである。
STDOUTで出力かSTDERRで出力かのふた通りがある

※python2 の確認
python2 -V 2>stderr

※python3 の確認
python3 -V 2>stderr

上記でコマンド実行時にコマンドライン上の出力にバージョン情報が出ていれば標準出力stdoutでバージョン情報を出しているpythonバージョン

cat stderr

でバージョン情報が見られるならstderrでバージョン情報をだすpythonバージョン

感想

複数の環境またいでいると、Shellゲーで地味にイラっとくるよ!

同じコマンドで結果が欲しいなら?

まぁ、面倒なので下記でいいのでは...?

python2 -V 1 > std  2>&1 & cat std
python2 -V 1 > std  2>&1 & cat std

参考: https://bugs.python.org/issue18338#msg192122

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?