Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-load a module on python startup

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.

like image 287
rubik Avatar asked Oct 23 '10 07:10

rubik


People also ask

How do you automatically import a module in Python?

Automatically add import statementsIn the Settings/Preferences dialog ( Ctrl+Alt+S ), click Editor | General | Auto Import. In the Python section, configure automatic imports: Select Show import popup to automatically display an import popup when tying the name of a class that lacks an import statement.

How do you load a module in Python?

To load the default version of Python module, use module load python . To select a particular software version, use module load python/version . For example, use module load python/3.5 to load the latest version of Python 3.5. After the module is loaded, you can run the interpreter by using the command python .

How many times does a module get loaded when imported multiple times in Python?

The rules are quite simple: the same module is evaluated only once, in other words, the module-level scope is executed just once. If the module, once evaluated, is imported again, it's second evaluation is skipped and the resolved already exports are used.


1 Answers

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.

  • http://docs.python.org/using/cmdline.html

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

like image 199
pyfunc Avatar answered Oct 14 '22 10:10

pyfunc