Can't instantiate class
Hello, 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 The DataUtil.py file resides in the same directory as the above file and looks like this: import MySQLdb class DataUtil: def __init__(self): print init def getConnection(self): return MySQLdb.connect(host=host, user=user, passwd=pass, db=test) 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? Thanks, Dave -- 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
Where to save classes? How to access classes?
Hi, I'm trying to get into the object oriented aspect of Python. If I create a custom class (in it's own file), how do I access that class in a function in a different file? In Java there's the notion of a CLASSPATH, where you can tell the compiler to look for classes. Is there something similar to this in Python? Thanks, Dave -- http://mail.python.org/mailman/listinfo/python-list
Problem With Insert with MySQLdb
Hello, I am a complete beginner with Python. I've managed to get mod_python up and running with Apache2 and I'm trying to a simple insert into a table in a MySQL database. I'm using the MySQLdb library for connectivity. I can read from the database no problem, but when I do an insert, the value never gets added to the database, even though there is no error, and the SQL is fine (I print out the SQL statement in the function). When I copy and paste the sql from my browser and insert directly into MySQL, it works fine. Here is the function in question: def add(req): db = MySQLdb.connect(host=intranet, user=root, passwd=, db=intranet) # create a cursor cursor = db.cursor() # execute SQL statement sql = INSERT INTO category (category_name) VALUES (' + req.form['category'] + ') cursor.execute(sql) return sql The SQL it is outputting is: INSERT INTO category (category_name) VALUES ('Test') Am I doing something obviously incorrect here? Thanks, Dave -- http://mail.python.org/mailman/listinfo/python-list