[Tutor] How to import a few modules when I enter python?

Peter Otten __peter__ at web.de
Tue Aug 7 09:05:45 CEST 2012


Santosh Kumar wrote:

> Hello there,
> 
> I have a few scripts that I made to experiment with, I have to import
> them everytime I enter the Python shell. The scripts are in
> `/home/username/workshop/` (this directory has also some non .py
> files) directory. Is there a way I can import them as soon as I enter
> Python?
> 
> Also I am exploring the OS module, can I import them as well?

You can specify a python module that will be executed at startup by setting 
the PYTHONSTARTUP environment variable:

$ echo 'print "Preparing..."
import os
import sys
foo = 42
> print "...done"' > startup.py
$ PYTHONSTARTUP=startup.py python
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Preparing...
...done
>>> foo
42
>>> os
<module 'os' from '/usr/lib/python2.7/os.pyc'>




More information about the Tutor mailing list