IPythonを使おう1
Pythonがインストールされていてば
% python
で
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
のような対話モードでPythonを操ることができるのですが、IPythonを使うとより幸せになれます。IPythonを使わない意味が分かりませんが、よく考えると僕自身IPythonを活用しきってないので、調査することにします
インストール
Windowsの場合
PyWin32,PyReadline,CTypes(python2.5以降は不必要)なので Installation を見てインストールしてください。
Linuxの場合
apt-getとかyumでインストールしましょう。
使い方
起動
コマンドラインから
% ipython
で
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 0.8.1 -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]:
このように立ち上がります。Windowsの場合はインストールの方法によっては
python c:\Python24\Scripts\ipython
のようにする場合があるかもしれません。面倒な場合は環境変数の設定をしてください。
コマンドを使う
まず ls, cd, mkdirなどのよく使うUnix系のコマンドを試してみましょう。
!を先頭につけてばOSのコマンドが使えます。
例、Windowsでc:tmpのディレクトリに移動して、hogeというディレクトリを作って消す
In [1]: cd c:\tmp
c:\tmp
In [2]: mkdir hoge
In [3]: ls #hogeディレクトリが出来ているのを確認, Windowsでもlsコマンドが使えます。
In [4]: !rmdir hoge
In [5]: ls #hogeディレクトリが削除されたことを確認
補完を使う
IPythonは補完が使えます
In [1]: import sys
In [2]: sys. #ここでTabキーを押すと候補がたくさん
sys.__displayhook__ sys.exc_info sys.modules
sys.__doc__ sys.exc_traceback sys.path
In [2]: sys.pa #paまで打ってからTabキーを押すとpaに続く補完候補が現れる
sys.path sys.path_hooks sys.path_importer_cache
モジュールのヘルプとソースを見る
?をつけることによってモジュールの情報を見ることができます。
In [1]: from StringIO import StringIO
In [2]: s = StringIO()
In [3]: s?
Type: instance
Base Class: StringIO.StringIO
String Form: <StringIO.StringIO instance at 0x00EF8828>
Namespace: Interactive
Docstring:
class StringIO([buffer])
When a StringIO object is created, it can be initialized to an existing
string by passing the string to the constructor. If no string is given,
In [4]: s.write?
Type: instancemethod
Base Class: <type 'instancemethod'>
String Form: <bound method StringIO.write of <StringIO.StringIO instance at 0x00EF8828>>
Namespace: Interactive
File: c:\python24\lib\stringio.py
Definition: s.write(self, s)
Docstring:
Write a string to the file.
There is no return value.
Baseクラスや、ファイルの情報が表示されてますね。
さらに??をつけるとソースコードを見ることもできます。
In [5]: s.write??
Type: instancemethod
Base Class: <type 'instancemethod'>
String Form: <bound method StringIO.write of <StringIO.StringIO instance at 0x00EF8828>>
Namespace: Interactive
File: c:\python24\lib\stringio.py
Definition: s.write(self, s)
Source:
def write(self, s):
"""Write a string to the file.
There is no return value.
"""
_complain_ifclosed(self.closed)
if not s: return
# Force s to be a string or unicode
if not isinstance(s, basestring):
s = str(s)
spos = self.pos
slen = self.len
if spos == slen:
self.buflist.append(s)
self.len = self.pos = spos + len(s)
return
if spos > slen:
self.buflist.append('\0'*(spos - slen))
slen = spos
newpos = spos + len(s)
if spos < slen:
if self.buflist:
self.buf += ''.join(self.buflist)
self.buflist = [self.buf[:spos], s, self.buf[newpos:]]
self.buf = ''
if newpos > slen:
slen = newpos
else:
self.buflist.append(s)
slen = newpos
self.len = slen
self.pos = newpos
入出力履歴と通常モードでの立ち上げ
通常の>>>ではなく、InとかOutとかがあると思いますが、これは入出力の記録になっています。入出力は呼び出すことができます。
In [1]: a = "hoge"
In [2]: a*3
Out [2]: 'hogehogehoge'
In [3]: In[1]
Out [3]: u'a = "hoge"\n'
In [4]: Out[2]
Out [4]: 'hogehogehoge'
しかし、これがあって嬉しいと思ったことはほとんどなく、DocTestが書きづらくなるので通常の>>>で立ち上げたいところです。その場合は-clオプションをつけましょう。
% ipython -cl
するとIn Outではなく見慣れた>>>で表示されます。補完もコマンドも履歴も普通に使えます。
>>> a = "hoge"
>>> a*3
'hogehogehoge'
>>> In[1]
u'a = "hoge"\n'
>>> Out[2]
'hogehogehoge'
>>> In
['\n', u'a = "hoge"\n', u'a*3\n', u'In[1]\n', u'Out[2]\n', u'In\n']
>>> Out
{2: 'hogehogehoge', 3: u'a = "hoge"\n', 4: 'hogehogehoge', 5: ['\n', u'a = "hoge"\n', u'a*3\n', u'In[1]\n', u'Out[2]\n', u'In\n', u'Out\n']}
>>>
続く(予定)
Comments
僕も最近ドキュメントを読んで知りました。それまでipythonrcをいじってました。




-cl は知らなかった