LoginSignup
3
4

More than 3 years have passed since last update.

gitbashでpythonの対話モードの使い方

Posted at

gitbashでpythonの対話モードの使い方

gitbashでpythonの対話モードに入る方法です。

Windowsのpowershellだとシンプルにpythonと入力すれば対話モードに入るもののgitbashでは入れない。

winpty

コマンド冒頭にwinpytyをつける。
 └ winpty pythonこれだけでOK

$ winpty python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 

対話モードから抜けるときはexit()
 └ カッコ必須

>>> exit()


ipython

pythonの対話モードで処理を実行するのであればipythonがおすすめ。
whileやfor文のインデントを自動でつけてくれる。(コードをコピペした場合もインデントが残る)

通常のpythonだとインデントは自分で逐一つけないとエラーになる。

ipythonのインストール方法

#ipythonのインストール
pip install ipython

#ipythonの起動
winpty ipython

#起動成功
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

通常のpythonの>>>In [行番号]:になる。

対話モードから抜けるときはexitまたはexit()
 └ カッコなくてもOK

In [1]: exit


pipとは(補足)

python公式のパッケージ管理システム。pythonの中にデフォルトで入っている。

pythonを使う上ではなにかと便利(というか必須)な機能。

>pipの詳細や主要コマンド一覧


ショートカットの作成

毎回winpty pythonwinpty ipythonと打つのが面倒なので、
エイリアス(ショートカットコマンド)を設定すると、対話モードに入るのが簡単になります。

>設定方法はこちら

慣れてしまえば設定は簡単で、gitbushを使うなら必須の機能。

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