the_drow
the_drow

Reputation: 19181

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 module of application foo?

Upvotes: 0

Views: 151

Answers (1)

DrMeers
DrMeers

Reputation: 4207

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__)

Upvotes: 2

Related Questions