error on importing variable value

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Dec 29 20:14:45 EST 2007


On 29 dic, 20:31, int32... at yahoo.com wrote:

> I can't figure out why this doesn't work. Any ideas appreciated.
>
> conn = MySQLdb.connect (db = "vocab")
> cursor = conn.cursor ()
> cursor.execute ("SELECT VERSION()")
> row = cursor.fetchone ()
> print "server version:", row[0]
> cursor.close ()
> conn.close ()
>
> gives:
>
> server version: 5.0.44-log
>
> but
>
> import defs
> conn = MySQLdb.connect (defs.connect)
> [...]
> where defs.py is
>
> connect = 'db = "vocab"'
>
> gives:
>
> Traceback (most recent call last):
>         _mysql_exceptions.OperationalError: (2005, 'Unknown MySQL server host
> \'db = "vocab"\' (3)')

Try this:

defs.py:
dbname = "vocab"

import defs
conn = MySQLdb.connect(db=defs.dbname)

BTW, please read the Style Guide at http://www.python.org/dev/peps/pep-0008
- in particular, I feel space before an opening parens rather
annoying. But it's just a matter of style.

--
Gabriel Genellina



More information about the Python-list mailing list