LoginSignup
6
1

More than 5 years have passed since last update.

【Python】--versionの出力先が2系と3系で違う問題

Last updated at Posted at 2018-01-30

Pythonの python --version をたたいた際に、2系と3系で出力先が違う。
そのためシェルでパイプしようとしたときに出力結果をうまく流せず困った。

確認

まずは、2系と3系の出力先を調べてみる。

Python2系の場合

# Pythonのバージョンを確認
$ python --version
Python 2.7.14

# 標準出力を除外 ➠ 出力あり
$ python --version 1>/dev/null
Python 2.7.14

# 標準エラー出力を除外 ➠ 出力なし
$ python --version 2>/dev/null

Python3系の場合

# Pythonのバージョンを確認
$ python --version
Python 3.6.4

# 標準出力を除外 ➠ 出力なし
$ python --version 1>/dev/null

# 標準エラー出力を除外 ➠ 出力あり
$ python --version 2>/dev/null
Python 3.6.4

結果

Python2系は、標準エラー出力

Python3系は、標準出力

とそれぞれ出力されている。

つまり出力をsedとかにパイプしたい場合は、
python --version 2>&1 | sed のように標準出力へまとめてあげれば、
2系と3系のどちらも対応ができそう。

6
1
2

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