When working on a project my scripts often have some boiler-plate code, like adding paths to sys.path and importing my project’s modules. It gets tedious to run this boiler-plate code every time I start up the interactive interpreter to quickly check something, so I’m wondering if it’s possible to pass a script to the interpreter that it will run before it becomes “interactive”.
When working on a project my scripts often have some boiler-plate code, like adding
Share
That can be done using the
-ioption. Quoting the interpreter help text:-i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=xSo the interpreter runs the script, then makes the interactive prompt available after execution.
Example:
This can also be done using the environment variable PYTHONSTARTUP. Example:
I personally prefer the former method since it doesn’t show the three lines of information, but either will get the job done.