I would like to play around in the python interpreter but with a bunch of imports and object setup completed. Right now I’m launching the interpreter on the command line and doing the setup work every time. Is there any way to launch the command line interpreter with all the initialization work done?
Ex:
# Done automatically.
import foo
import baz
l = [1,2,3,4]
# Launch the interpreter.
launch_interpreter()
>> print l
>> [1,2,3,4]
You can create a script with the code you wish to run automatically, then use
python -ito run it. For example, create a script (let’s call it script.py) with this:Then run the script
After the script has completed running, python leaves you in an interactive session with the results of the script still around.
If you really want some things done every time you run python, you can set the environment variable
PYTHONSTARTUPto a script which will be run every time you start python. See the documentation on the interactive startup file.