What about an admin.py in an egg that legitimately raises an error on
import? If imp.find_module can't find it as you claim, won't that
error be eaten? Now, I realize that should probably be caught while
testing not in an egg first, but it is a real possibility. Perhaps we
shouldn't care about that one though.
--
----
Waylan Limberg
way...@gmail.com
>
> Could always use the super hacky "check the ImportError message for
> the name of the admin module" to decide whether to reraise the error
> or not.
Please don't.
The solution lies within the fourth argument to __import__.
The statement
from x.y import z
equates to
z = __import__("x.y", {}, {}, ["z"]).z
(I think you can guess what " as q" changes.)
So, what we want the autodiscover to do is to run
from <appname> import admin
which would be
mod = __import__(appname, {}, {}, "admin")
And that should be enough, since the admin registering machinery
should caretake the rest-- the module has been imported. Proof:
>>> __import__("django.core", {}, {}, ["files"]).files
<module 'django.core.files' from '.../django/core/files/__init__.pyc'>
-- Restart CPython --
>>> __import__("django.core", {}, {}, [""]).files
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'files'
Hope that made sense,
Ludvig
I should proof-read more.
from <appname> import admin
is the same as
admin = __import__(appname, {}, {} ["admin"]).admin
_but_ our interest lies within executing the code, so this is enough:
__import__(appname, {}, {} ["admin"])
Without any try/except clause, because Python gives no error if you
specify a from_name (fourth argument) which doesn't exist. So that's
really all you need.