Class context execution problems

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Mar 6 14:46:04 EST 2008


En Thu, 06 Mar 2008 11:04:26 -0200, Marcelo de Moraes Serpa  
<celoserpa at gmail.com> escribi�:

> I'm using a class in a conext other than the subpackage in which it is
> located and I'm getting template lookup errors.
>
> This class expects to find a .pt file in a directory relative to its
> package. However, this class is normally used by other classes in the  
> same
> namespace.
>
> class ObjectWidget:
>      template = "templates/objectwidget.pt"
>
> However, I needed to import this class (ObjectWidget) from a class  
> outside
> its namespace(package). Now, I think the class is running in the context  
> of
> the file that is using it and so, the
> the class not finding the .pt file (I think becouse the class file is  
> being
> contextualized to the currently running one which is outside the browser
> subpackage)

No, it's not. The globals() (i.e., the "context") are of the imported  
module itself, not of the caller. The error probably lies on how that  
template attribute is converted into a full path; post that code.
As an example, this is a rather safe way, even if the program uses  
os.chdir():

basepath = os.path.abspath(os.path.dirname(__file__))

class ObjectWidget:
     template = "templates/objectwidget.pt"

     def open(self):
         fn = os.path.join(basepath, template)
         print fn

-- 
Gabriel Genellina




More information about the Python-list mailing list