How can I start the python shell and automatically initialize it with some commands?

Here's a way without having to mess with environment variables:

For example, if I had a script with the following in it called script.py:

#!/usr/bin/env python
print("example")

I could tell python to run this before bringing me to the interpreter with the -i flag.

$ python -i script.py
example
>>> 

I think you're looking for the PYTHONSTARTUP environment variable


I'd suggest you to use IPython, if possible. It gives tones of great features, and autoexec is only one of them. But of course, correct answer is mentioned by @mgilston

Tags:

Python