Is it possible to get Interactive Interpreter Python to run a script at boot time?

When working on a project, my scripts often have boiler room code, for example, adding paths to sys.path and importing my project modules. It is very difficult for me to run this boiler plate code every time I start the interactive interpreter in order to quickly check something, so I wonder if it is possible to pass the script to the interpreter that it will run before it becomes β€œinteractive”.

+5
source share
1 answer

This can be done using the option -i. Quoting translator help text:

-i: inspect interactively after running script; forces a prompt even
         if stdin does not appear to be a terminal; also PYTHONINSPECT=x

, script, .

:

$ python -i boilerplate.py
>>> print mymodule.__doc__
I'm a module!
>>>

, PYTHONSTARTUP. :

$ PYTHONSTARTUP=boilerplate.py python
Python 2.7.3 (default, Sep  4 2012, 10:30:34) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print mymodule.__doc__
I'm a module!
>>>

, , .

+6

All Articles