Re: Can't instantiate class
On 11/6/05, David Mitchell [EMAIL PROTECTED] wrote: import DataUtil File C:\Apache2\htdocs\Intranet\addLink.py, line 42, in getCategories db = DataUtil() TypeError: 'module' object is not callable You've imported module DataUtil, and by calling DataUtil(), you're trying to call the module, hence the error. I think you want db = DataUtil.DataUtil() Or, from DataUtil import DataUtil And then your code will work. Mike -- Michael P. Soulier [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Can't instantiate class
David Mitchell wrote: Here is a very basic question, but it is frustrating me to no end nonetheless. I have one file called addLink.py. In a method in this file I am trying to instantiate a class and call a method from that class. Here is the code: def getCategories(): # instantiate the DataUtil class db = DataUtil() # and call the getConnection() method connection = db.getConnection() ... At the top of this file I am importing the DataUtil module (named DataUtil.py) with this line: import DataUtil When I execute the getCategories() method above I get the following error: File C:\Apache2\htdocs\Intranet\addLink.py, line 42, in getCategories db = DataUtil() TypeError: 'module' object is not callable Any idea what I'm doing wrong? the error message contains the full story: when you do import DataUtil, you get an object named DataUtil that represents the module. (import is not an include statement). and modules are not callable. to access the DataUtil object *inside* the DataUtil module, use import DataUtil db = DataUtil.DataUtil() or from DataUtil import DataUtil db = DataUtil() also see: http://effbot.org/zone/import-confusion.htm /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Can't instantiate class
Thanks for your prompt reply. Ok, so If use your first suggestion (db = DataUtil.DataUtil() ), I get this error: AttributeError: 'module' object has no attribute 'DataUtil' If I try importing the class directly (from DataUtil import DataUtil), I get this error: ImportError: cannot import name DataUtil Could these errors have something to do with the fact that I am doing this through mod_python? Thanks again, Dave Michael P. Soulier wrote: On 11/6/05, David Mitchell [EMAIL PROTECTED] wrote: import DataUtil File C:\Apache2\htdocs\Intranet\addLink.py, line 42, in getCategories db = DataUtil() TypeError: 'module' object is not callable You've imported module DataUtil, and by calling DataUtil(), you're trying to call the module, hence the error. I think you want db = DataUtil.DataUtil() Or, from DataUtil import DataUtil And then your code will work. Mike -- Michael P. Soulier [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Can't instantiate class
David Mitchell wrote: Ok, so If use your first suggestion (db = DataUtil.DataUtil() ), I get this error: AttributeError: 'module' object has no attribute 'DataUtil' If I try importing the class directly (from DataUtil import DataUtil), I get this error: ImportError: cannot import name DataUtil add import DataUtil print DataUtil.__file__ print dir(DataUtil) and make sure that you're really importing the module you think you're importing (the first print statement prints the module file- name, the second the contents) /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Can't instantiate class
On Sun, 06 Nov 2005 09:47:04 -0500, David Mitchell [EMAIL PROTECTED] wrote: Thanks for your prompt reply. Ok, so If use your first suggestion (db = DataUtil.DataUtil() ), I get this error: AttributeError: 'module' object has no attribute 'DataUtil' Have you looked to see what DataUtil you are importing? E.g., after import DataUtil, put a print repr(DataUtil.__file__) (is there a .pyc shadowing the .py you want? Is it from an unexpected directory? Have you looked at the search path that is in effect when you import? E.g., to print a list of the paths searched to find DataUtil.pyc (or if nonexistent or not up to date, DataUtil.py), you could do this import sys for p in sys.path: print p And then have you checked whether the above error message is telling the truth, i.e., that indeed DataUtil does not define DataUtil.DataUtil? Try doing help(DataUtil). If I try importing the class directly (from DataUtil import DataUtil), I get this error: ImportError: cannot import name DataUtil Sure, the first error message would predict the latter one ;-) Could these errors have something to do with the fact that I am doing this through mod_python? Could be, yes. I haven't used it, but I would guess it's a possibility. A server will generally be set up to run in a different environment than your normal login environment, so it's possible/probable that it has a different sys.path than you normally have, and even if not textually different, if the first element is '' to indicate current working directory that will generally be a different working from your normal login directory, depending on server config for responding to particular urls. It's possible to set up apache to run cgi impersonating a particular user account instead of the usual nobody or such (which generally has restricted file access), but it's probably nobody or some special user/group designed for security purposes, so you might want to check permissions on the files the server is supposed to be able to access for r, w, or x. There should be some standard test cgi stuff that will tell you about the environment. And hopefully also some wrapper to catch exceptions that might otherwise silently get lost (or show up in server error logs -- have you looked there?). You might want to put a try/except around your whole code, and burp out some carefully legal message page for the browser in case you catch something, e.g., if there were some exception in the DataUtil class body that prevented DataUtil.DataUtil from being defined. (And look for bare except: clauses or other exception handling that might be throwing away a DataUtil definition exception). HTH Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list