I have a index.py file which imports a few modules. I later import my own module, which needs some of the earlier imports. How can I import the modules I need only once, and make them available to all subsequent user modules I build, without importing all the same stuff in my different user modules?
index.py
import os
import random
import myusermodule
import myotherusermodule
...
myusermodule.py
print random.randrange(0,1)
myotherusermodule.py
print random.randrange(0.5)
How can I avoid having to import random in my 2 user modules, and simply have it in the index.py file?
thanks
You don’t need to worry about this at all. Python caches module loads in sys.modules so it doesn’t hurt to import the required at the top of each of your custom modules, and is in fact, the recommended and pythonic way.
Further information: http://docs.python.org/tutorial/modules.html – section 6.1 specifically, but is worth a read.