Error importing module from within a virtual method field defined in another module

19 views
Skip to first unread message

Lisandro

unread,
Apr 21, 2016, 7:48:06 AM4/21/16
to web2py-users
I have a virtual method field, and its associated function is defined inside a module.
That function imports another function defined in another module.
And that last function also imports another module.


The virtual method function works ok when I call it from a controller function.
However, if I call that method in the scheduler environment, I have the error "Cannot import module mercadopago"


The virtual method field is defined in db.py like this:
from virtual_methods import set_metodo_pago

db
.define_table('sitios',
 
Field('nombre'),
 
Field.Method('set_metodo_pago', lambda row, metodo_pago: sitios_set_metodo_pago(row, metodo_pago)))


These are my modules:
modules/virtual_methods.py
modules
/globals.py
modules
/mercadopago.py
modules
/scheduler_functions.py


in modules/virtual_methods.py
def set_metodo_pago(row, metodo_pago):
   
from globals import get_mercadopago_client
    mp
= get_mercadopago_client()
   
...



in modules/globals.py
def get_mercadopago_client():
   
import mercadopago
   
return MP.client()
   
...


in modules/scheduler_function.py
def instalar_nuevo_sitio(nombre):
   db
= current.db
   sitio
= db(db.sitios.nombre == nombre).select().first()
   sitio
.set_metodo_pago(metodopago='ticket') # this is the line that throws the error


However, the same line works ok if I call it from a controller/function.
What am I missing?

Lisandro

unread,
Apr 21, 2016, 10:25:53 AM4/21/16
to web2py-users
I've "fixed" the problem, but I don't understand exactly what was it.

The problem wasn't the import itself, but the moment in wich I was doing it.

Let's go back to the function that had the error, here with more detail

def instalar_nuevo_sitio(nombre):
    sitio = db(db.sitios.nombre == nombre).select().first()

   
# here it does a lot of stuff
    sitio
.set_metodo_pago(metodopago='ticket')  # this line throws the error


Notice the comment where I say that in that place, the function does "a lot of stuff". What is that stuff? Well, what the function does is to install a new app in web2py, that is, clonning the repository and putting all the files needed by the new app. 

How did I solve it? I just moved the sentence to the top, before doing that "lot of stuff".
I don't know why exactly that worked, because the line that throwed the error doesn't have anything to do with the new application installed by the function.

Anyway, It's solved now, but I cannot say why :/
Reply all
Reply to author
Forward
0 new messages