I have a folder in my Application directory called Commands.folder. What I want to do is import all the modules in that folder, regardless of the name, into the python file that imports. How can I do this?
+2
A:
If you start your program in Application, you can import all modules in /Commands using:
from Commands import *
Ian Wetherbee
2010-07-20 03:46:44
+2
A:
from Commands import *
You should create an empty file named "__init__.py
" in the "Commands" folder, and your main app script should be in the "Application" folder you've mentioned.
Note however, the "from module import *" is not recommended since it may cause namespace pollution.
Read this.
Wang Dingwei
2010-07-20 04:03:16
Thank you for pointing out that very important file requirement. Is there anything I can do to customize the init file?
Galilsnap
2010-07-20 05:59:48
Yes. You can specify which modules to include by adding a list named '__all__' in it. See Python documentation: http://docs.python.org/tutorial/modules.html#importing-from-a-package
Wang Dingwei
2010-07-20 07:12:41