Friday, September 10, 2010

Re: How does django convert string to modules

What if was gibing a string like this: 'math.ceil', and i had to import the function(not the module) dynamically(from the string), how can i do that? import_module is for importing... modules, not functions.

2010/9/10 Bachir <bachir009@gmail.com>
thanks, éthat's really an awesome explanation. thanks

2010/9/10 Tom Evans <tevans.uk@googlemail.com>

On Fri, Sep 10, 2010 at 1:05 AM, Bachir <bachir009@gmail.com> wrote:
> The problem is not the lack of information, the real problem is that i don't
> know what to search for .
>
> 2010/9/9 bruno desthuilliers <bruno.desthuilliers@gmail.com>
>>
>>
>> On 9 sep, 03:46, maroxe <bachir...@gmail.com> wrote:
>> > Hi, I am trying to understand an other magic thing about django: it
>> > can convert strings to modules. In settings.py, INSTALLED_APPS is
>> > declared like that:
>> >
>> > INSTALLED_APPS = (
>> >     'django.contrib.auth',
>> >     'django.contrib.contenttypes',
>> >     'django.contrib.sessions',
>> > )
>> > All it contains is strings. But django will convert those strings to
>> > modules and import them later.
>> >
>> > I want to do be able to do the same thing. but i don't know how.

Well, OK. Lets break down how one could find this out, because you
*did* have everything you needed to find this out yourself.

You wanted to turn a string into a module name, so you can import from it.
You noticed that django does the same thing with the setting INSTALLED_APPS.
You have the django source code.

So, the first thing to do is to search the django source code for
INSTALLED_APPS.
For this, I tend to use "ack", which is like grep but better for programmers.
This returns lots of files - its used in lots of places. Look at the
list of files returned and choose one that looks appropriate to look
at.

I chose "core/management/commands/syncdb.py", as I knew this file is
responsible for creating db models - its the code that runs when you
run ./manage.py syncdb.

Looking at how INSTALLED_APPS is used in that file:

>        for app_name in settings.INSTALLED_APPS:
>            try:
>                import_module('.management', app_name)
>            except ImportError, exc:

Ah, so theres a function called import_module. Where does that come from?

> from django.utils.importlib import import_module

Ah, so now I know how it does it's import magic, and I know how to
import it myself. I can now play around with this in the shell, so I
know I have the usage right:

>>> from django.utils.importlib import import_module
>>> mod = import_module('math')
>>> mod.ceil
<built-in function ceil>

There, not so hard, is it? You did know what to search for, as you
included it in your original question..

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment