How can I get Django application name for a file inside that application?
Question: Having full path for the file that is a part of django application I would
like to get a Django application name.

For example for this path:

    
    
    /lib/python2.6/site-packages/django/contrib/auth/tests/auth_backends.py
    

Application name is `auth`.

I wonder how this Django application name could be programatically calculated
for specific filename inside an app.

Background: I want to integrate calling Django test management command from a
vim editor. It should run tests for an app currently edited file belongs.


Answer: You can get module by filename with
[`__import__`](http://docs.python.org/library/functions.html#__import__). You
can get it's package name with `__package__` attribute. Then you can check
which app from `settings.INSTALLED_APPS` is a substring of package name. It's
your app.

