(Important: See update below.)
I'm trying to write a function, import_something
, that will important certain modules. (It doesn't matter which for this question.) The thing is, I would like those modules to be imported at the level from which the function is called. For example:
import_something() # Let's say this imports my_module
my_module.do_stuff() #
Is this possible?
Update:
Sorry, my original phrasing and example were misleading. I'll try to explain my entire problem. What I have is a package, which has inside it some modules and packages. In its __init__.py
I want to import all the modules and packages. So somewhere else in the program, I import the entire package, and iterate over the modules/packages it has imported.
(Why? The package is called crunchers
, and inside it there are defined all kinds of crunchers, like CruncherThread
, CruncherProcess
, and in the future perhaps MicroThreadCruncher
. I want the crunchers package to automatically have all the crunchers that are placed in it, so later in the program when I use crunchers
I know it can tell exactly which crunchers I have defined.)
I know I can solve this if I avoid using functions at all, and do all imports on the main level with for
loops and such. But it's ugly and I want to see if I can avoid it.
If anything more is unclear, please ask in comments.