Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import modules when starting python in Windows [duplicate]

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.

like image 874
Zhang Yunjie Avatar asked Jun 14 '26 11:06

Zhang Yunjie


2 Answers

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
like image 70
gregb212 Avatar answered Jun 17 '26 01:06

gregb212


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.

like image 35
AChampion Avatar answered Jun 17 '26 00:06

AChampion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!