LoginSignup
1
2

More than 5 years have passed since last update.

Python2.7 on Windowsでファイル名だけでPythonスクリプトを実行した際にsys.argvが渡されない問題の対処法

Last updated at Posted at 2016-03-22

問題点

Python2.7 on Windowsの環境で、
たとえば以下のようなtest.pyがあったとして

import sys

print(len(sys.argv))

引数付きでコマンドプロンプトから実行してもsys.argvが常に1になってしまう。

> test.py a b c
1 // <= NG: 本来は4であるべき

なおpythonコマンドから実行すると問題ない。

> python test.py a b c
4 // <= OK

対処方法

https://bugs.python.org/issue7936 にissueとして上がっていた。
結論としてはレジストリーをいじる。

regeditを立ち上げて、 HKEY_CURRENT_USER\Software\Classes\Applications\python.exe\shell\open\commandに移動

スクリーンショット 2016-03-22 午後5.57.40.png

この値が"C:\Python27\python.exe" "%1"になっている場合は、"C:\Python27\python.exe" "%1" %*に修正する。

これで正しくsys.argvが渡されるようになるはず。

> test.py a b c
4 // OK

それにしても6年前に報告されたバグなので、さすがに修正されててほしかった。

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