0

In Django:

a) What is the best way to test that another app is installed? (By installed I mean to be in INSTALLED_APPS)

b) What is the recommended way to alter the behaviour of the current app accordingly. I understand that:

if "app_to_test" in settings.INSTALLED_APPS:
  # Do something given app_to_test is installed
else:
  # Do something given app_to_test is NOT installed

is possible, but is there another way? is this the recommended way?

c) What is the recommended practice to import modules that are only required if another app is installed? import then inside of the if block that test for the installed app?

1
  • This is a Django question. In Django an app is a self contained group of modules that integrates within the Django framework. I guess the Python tag is not ideal. I will remove it. Commented Oct 1, 2013 at 2:00

2 Answers 2

3

I tend to favour checking INSTALLED_APPS as you have listed in your question.

if DEBUG and 'debug_toolbar' not in INSTALLED_APPS:
    INSTALLED_APPS.append('debug_toolbar')
    INTERNAL_IPS = ('127.0.0.1',)

This works well when you have settings distributed across different settings files that don't necessarily have knowledge of the other. eg I might have a shared_settings.py which contains a base set of INSTALLED_APPS, then a debug_settings.py which imports shared_settings.py and then adds any additional apps as required.

The same applies for non-settings. For example, if you have Django South installed and want to create introspection rules for South only if it's installed, I would do this:

if 'south' in settings.INSTALLED_APPS:
    from south.modelsinspector import add_introspection_rules
    # Let South introspect custom fields for migration rules.
    add_introspection_rules([], [r"^myapp\.models\.fields\.SomeCustomField"])

As I see it, there's no need to try and import a module if you know there's the possibility that it may not be installed. If the user has listed the module in INSTALLED_APPS then it is expected to be importable.

Sign up to request clarification or add additional context in comments.

Comments

2

That?

try:
  # Test it
  from an_app import something 
except ImportError as e:
  from another_app import something
  #Do something else

6 Comments

Is this the standard way? Are there some Django's functions that do something equivalent?
Also, I want to test if an app is installed. Not if the module is importable.
@papirrin - this is the recommended way. You can import a module only if an app is installed.
@karthikr - No, it is completely possible to import a module regardless of whether its app is installed or not. By installed apps I mean django INSTALLED_APPS, no Python modules.
That is exactly what i am talking about. IN django, app is installed means it is in the INSTALLED_APPS
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.