I'm using python on Windows. And I'm trying to find a way to import some default module when starting python. It means, when starting python, some modules should be already imported, just like builtins. Is there any way?
Thanks.
If you mean while going into the interactive mode, just make a small startup script. Instead of just launching python with:
python
try instead:
python -i startupImports.py
startupImports.py:
import time
from collections import defaultdict
from customclass import myclass
#...etc
Define a PYTHONSTARTUP environment variable and point it to a python file and it will be loaded when you start python interactively without a command line script.
I have my PYTHONSTARTUP point to a .pythonrc where I try to import a __boot__.py (bootstrap) in the current directory for any interactive python session.
try:
from __boot__ import *
except Exception:
pass
There's a bunch of other things in my pythonrc like __future__ imports, readline setup, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With