15

I want IPython or the Python interpreter to auto-load a module when I start them.

Is it possible?

For example when I start IPython:

$ ipython

...

>>> from __future__ import division
>>> from mymodule import *

In [1]:

Something like SymPy's live shell found in the tutorial pages.

4

5 に答える 5

17

Have a .pythonstartup in your home directory and load modules there and point PYTHONSTARTUP env to that file.

Python commands in that file are executed before the first prompt is displayed in interactive mode.

I use it for enabling command line completion in python interpreter shell

于 2010-10-23T07:18:34.997 に答える
6

オプションがバイナリに-S渡されない限り、実行がスクリプトまたは対話型インタープリターに渡される前に、デフォルトで特別なサイトモジュールがインポートされます。とりわけ、モジュールはファイルを探します。各行には、ファイルに含めるパスまたは実行するコマンドのいずれかが含まれている必要があります。このモジュールは, and (任意のコードを含む可能性があり、エラーが発生した場合に同僚を狂わせる良い方法です)もインポートします(これらが のどこかに存在する場合) 。python*.pth*.pthsys.pathsitecustomizeusercustomizesys.path

sys.pathただし、問題は、モジュールがインポートされたときに現在のディレクトリが含まれていないsiteことです。つまり、特定のスクリプトを構成するのが難しいということです。

スクリプトの先頭に次の行を追加することがあります。これにより、スクリプトは.pth現在のディレクトリ内のファイルを検索し、不足しているパスを に追加しますsys.path

# search for *.pth files in the current directory
import site; site.addsitedir('')
于 2013-09-23T17:19:25.440 に答える
4

Check the file ~/.ipython/ipythonrc - you can list all modules you want to load at the startup.

于 2010-10-23T07:17:40.567 に答える