LoginSignup
7
1

More than 5 years have passed since last update.

Python のインタラクティブモードが、とてつもなく遅い

Last updated at Posted at 2018-04-19

いつからか、記憶にないくらい、
python (システム標準の v2.7 も、brew で入れた v3 系も)
起動および処理が、すっっっっっごく遅くなってしまった・・・。

TL;DR;

対話型 python が遅くなった時は、
~/.pythonhistory の肥大化を疑いましょう。

If your interactive python become too slow,
you should check size of your ~/.pythonhistory.

ことの始まり。

$ python
(数十秒!まつ)
>>> 1 + 1
(数十秒!まつ)
2
>>> ^D
(数十秒!まつ)

orz;
python 使うことすら嫌になってくる。

なのに、こうすると、さくっと返ってくる・・

$ python -V
Python 2.7.10
(さくっ)
$ python -c "print(1 + 1)"
2
(さくっ)

試行錯誤

解せない・・と思いつつ、数ヶ月 (一年以上) 放置。

ときおり思い出して
アンチウイルスソフトか と思い、
リアルタイムスキャンオフにしても変わらない。

Python つかいやすいけれど、
手元では使いづらいからなぁ、
いっそのこと Java (本末転倒) とか Go (これは勉強になった) で済ませるかぁ・・
なんて諦めていたのですが・・ (試行錯誤じゃない、という気もする)

光明

なんとはなしに見つけた環境変数を無効化してみたら・・・

$ env | grep -i python
PATH=/opt/local/bin:(snip)
PYTHONSTARTUP=/Users/xxxx/.pythonstartup

PYTHONSTARTUP

$ PYTHONSTARTUP= python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
(さくっ)

これか!!!

$ echo $PYTHONSTARTUP
/Users/xxxxx/.pythonstartup

$ cat $PYTHONSTARTUP
# -*- encoding: utf-8 -*-
# Python startup
import readline
import rlcompleter
import atexit
import os

readline.parse_and_bind('tab: complete')
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
    readline.read_history_file(histfile)
except IOError:
    pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter, atexit

ほほぅ・・・

$ ls ~/.pythonhistory 
/Users/xxxxx/.pythonhistory
$ wc -l ~/.pythonhistory 
 76871120 /Users/xxx/.pythonhistory
$ ls -l ~/.pythonhistory 
-rw-------  1 xxxxx  staff  1018138828  4 19 22:41 /Users/xxxxx/.pythonhistory

ほほほぅ・・・
お前か。ヒストリで 1G 近いのは。

$ cat /dev/null > ~/.pythonhistory 

・・・・
さて。

$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
(さくっ!!)

久しぶりに平和な世界に戻れました。

Conclusion

(再掲。refrain)

対話型 python が遅くなった時は、
~/.pythonhistory の肥大化を疑ってみましょう。

If your interactive python become too slow,
check size of your ~/.pythonhistory.

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