Say I want to print all my installed apps their version info on server startup.. I have this setup:
Project
/app-one
__init__.py
otherstuff
/app-two
__init__.py
otherstuff
/__init__.py
/admin.py
/urls.py
/settings.py
main init file
import settings
if settings.DEBUG:
for app in settings.INSTALLED_APPS:
try:
import app
print getattr(app, '__version__', None)
except Exception:
pass
app init file(s)
__version_info__ = ('0', '0', '1')
__version__ = '.'.join(__version_info__)
I get into the pass statement.. I suppose this is because the way instances work in Python, but how would I fix it?
this works though:
import app
getattr(app, '__version__', None)
This fixed it:
import settings
if settings.DEBUG:
for app in settings.INSTALLED_APPS:
app = __import__(app)
print getattr(app, '__version__', None)
appin your loop is not a module but a string. To load module by name you have to usedjango.utils.importlib.import_modulefunction: