How to create a django application which requires a specific module to run?
I have an application that requires the user to have a certain directory structure much like django does with templatetags and what I would like to know is how do I import an application named foo from a django project at runtime?
Furthermore if the application exists how do I import a specific modu开发者_如何学运维le of application foo?I think your question requires a few clarifying details. At the moment it seems as simple as putting the application (do you mean a django application?) on your pythonpath (e.g. inside the project directory):
try:
import foo
except ImportError:
pass # application mustn't be on pythonpath
else:
from foo import specific_module
# do stuff
Depending on your requirements, this code could be in a view, or even your project's __init__.py
if you want it to happen quite early. Improvements to django's start-up process are coming soon -- keep an eye out for startup.py
features.
If you need to import modules with dynamic names, you might want to look into __import__
You also have an helper method within django that is called django.utils.importlib.import_module (which uses __import__
)
精彩评论