Hello,

The module was not compiled, because it was the __import__ function
itself that raised an exception and so didn't have the chance to do
the compilation.

As you've suggested, I tried:

klass = __import__("myapp", {}, {}, [''])
-> Works, returns <module myapp>

klass = __import__("myapp.forms", {}, {}, [''])
-> Works, returns <module myapp.forms>, and compiles "forms.pyc"!!

klass = __import__("myapp.forms", {}, {}, ['MyModelForm'])
-> Works, and returns the same thing as above: <module myapp.forms>

But, although the module is now compiled, the following still doesn't
work:
klass = __import__("myapp.forms.MyModelForm", {}, {}, [''])

For info, MyModelForm is an instance of ModelFormMetaclass. I also
tried importing another model, still in vain:
klass = __import__("myapp.models.MyOtherModel", {}, {}, [''])


Looks like we're getting closer. It doesn't want to import the class,
although it can import all of the modules... Strange!!


On Feb 13, 12:45 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Tue, 2008-02-12 at 17:41 -0800, Julien wrote:
> > Hi!
>
> > I can't try this out at the moment as I don't have access to the code.
> > But I just thought I'd add the fact that forms.py is never compiled
> > (no trace of a forms.pyc). Should the __import__ function force the
> > compilation of a module when it's imported, or does it require that
> > module to be compiled prior (and if so, how can you force that
> > compilation)?
>
> The .pyc file thing is a red herring here. Python will byte compile
> files it imports and save them to disk, if it can (i.e. if it has write
> permission). However, if it cannot do so, no problems, it's just a bit
> slower the next time (because it has to recompile).
>
> So if you're in a position where a .pyc file loads and the
> corresponding .py file does not, something is seriously messed up. I
> doubt that's going on.
>
> That would lend evidence to my wondering if you are running the
> __import__ statement from something that is being imported by forms.py
> (so forms.py hasn't finished importing at the time you execute that line
> and, thus, forsm.MyModelForm wouldn't exist).
>
> Still, a quick test here shows that __import__ does save the .pyc file
> when the calling process had write-permission to the directory I was
> importing from.
>
> Regards,
> Malcolm
>
> --
> Quantum mechanics: the dreams stuff is made 
> of.http://www.pointy-stick.com/blog/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to