Python の対話型コマンド ラインは、スタートアップ ファイルを通じて設定できます。
Python が起動すると、環境変数 PYTHONSTARTUP が検索され、変数で指定されたファイル内のプログラム コードが実行されます。指定するファイル名とアドレスは任意です。 Tab キーを押すと、コンテンツとコマンド履歴が自動的に完了します。これはコマンド ラインに対する効果的な機能強化であり、これらのツールは readline モジュールに基づいて実装されます (実装を支援するには readline ライブラリが必要です)。
これは、Python コマンド ラインに主要なオートコンプリート機能と履歴コマンド機能を追加する簡単な起動スクリプト ファイルの例です。
[python@python ~]$ cat .pythonstartup import readline import rlcompleter import atexit import os #tab completion readline.parse_and_bind('tab: complete') #history file 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
環境変数を設定する
[python@python ~]$ cat .bash_profile|grep PYTHON export PYTHONSTARTUP=/home/python/.pythonstartup
タブの完了と履歴コマンドの表示を確認します。
[python@python ~]$ python Python 2.7.5 (default, Oct 6 2013, 10:45:13) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import md5 >>> md5. md5.__class__( md5.__getattribute__( md5.__reduce__( md5.__subclasshook__( md5.__delattr__( md5.__hash__( md5.__reduce_ex__( md5.blocksize md5.__dict__ md5.__init__( md5.__repr__( md5.digest_size md5.__doc__ md5.__name__ md5.__setattr__( md5.md5( md5.__file__ md5.__new__( md5.__sizeof__( md5.new( md5.__format__( md5.__package__ md5.__str__( md5.warnings >>> import os >>> import md5
注: make 中に表示される場合:
Python build finished, but the necessary bits to build these modules were not found: _tkinter gdbm readline sunaudiodev
これが無視された場合、import readline はエラーを報告します。モジュールが指定されていないことを示します。
欠落している指定されたパッケージは次のとおりです:
redhat: readline-devel.xxx.rpm
インストール後に再コンパイルして実行すると、問題は解決します。