Great!  "from mymodule import my_class" works exactly as you
described.

I didn't realise local_import was deprecated.  The web2py Book still
refers to it in this section:
http://web2py.com/book/default/chapter/04#Third-Party-Modules

You were right that something else was going on. I was invoking module
code from unit tests and had a typo in the import name.

Thanks for your help.


On Jun 29, 2:03 am, Massimo Di Pierro <massimo.dipie...@gmail.com>
wrote:
> Are you telling us that this does not work?
>
> mymodule = local_import('mymodule')
> my_new_class_instance = mymodule.my_class()
>
> It is veyr odd and I do not think this has anything to do with
> local_import. Something else is going on.
> Anyway, local_import is deprecated. Try
>
> from mymodule import my_class
>
> this should work now even if mymodule is in applications/yourapp/
> modules/
>
> On Jun 28, 8:54 am, Robert Clark <robert.cl...@niftybean.com> wrote:
>
>
>
>
>
>
>
> > I can't seem to get local_import() importing classes from module files
> > in the same way 'from X import Y' normally works in Python.  Say I
> > have the following in mymodule.py
>
> > def my_func(arg1):
> >     doSomething(arg1)
>
> > class my_class(object):
> >     __init__(self):
> >         ...etc.
>
> > my_class_ref = my_class    # Hack explained below
>
> > Importing my_func from a controller is fine:
>
> > mymodule = local_import('mymodule')
> > mymodule.my_func(some_arg)
>
> > But how can I get a reference to my_class for instantiation?  Normal
> > python importing would allow me to do this:
>
> > from mymodule import my_class
> > my_instance = my_class()
>
> > Some kind of similar syntax for local_import() would be helpful.  My
> > current 'Hack' is to add a variable (see "my_class_ref" above).
> > local_import allows those to be referenced from the imported module.
> > For example this works:
>
> > mymodule = local_import('mymodule')
> > my_new_class_instance = mymodule.my_class_ref()
>
> > Is there something I'm missing?  Any recommendations appreciated.

Reply via email to