global name 'MySQLdb' is not define d

1,551 views
Skip to first unread message

oktay

unread,
Jul 24, 2010, 11:46:36 AM7/24/10
to web2py-users
Hello,

I am trying to install web2py with apache & mysql.
I checked all the scenarios with the "global name 'MySQLdb' is not
defined" subject, but they didn't solve my problem.

I am using:
CentOS 5.5
httpd-2.2.3
mysql 5.0.77
Python26
MySQL-python-1.2.3c1
web2py Version 1.81.5

Default CentOS 5 python version is 2.4, I installed 2.6.
I can both "import MySQLdb" from python24 & python26 from console.
When I run web2py from source like "python ./web2py.py" or with
"python26 ./web2py.py"
I can see "Database drivers available: MySQL"

I think there is a configuration problem with the python libraries I
installed.

Do you have any idea? Here is the trace:

---------------
Traceback (most recent call last):
File "gluon/restricted.py", line 178, in restricted
exec ccode in environment
File "/var/www/web2py/applications/welcome/models/db.py", line 1, in
<module>
db=SQLDB('mysql://xxx:yyy@zzz:3306/aaa')
File "gluon/sql.py", line 921, in __init__
self._pool_connection(lambda : MySQLdb.Connection(
File "gluon/sql.py", line 828, in _pool_connection
self._connection = f()
File "gluon/sql.py", line 921, in <lambda>
self._pool_connection(lambda : MySQLdb.Connection(
NameError: global name 'MySQLdb' is not defined
----------------

mdipierro

unread,
Jul 24, 2010, 11:52:44 AM7/24/10
to web2py-users
As a test, can you try add

import MySQLdb

at the top of web2py.py and run

python2.6 web2py.py

Massimo

oktay

unread,
Jul 24, 2010, 11:58:36 AM7/24/10
to web2py-users
Yes, it works with "import MySQLdb" at the top of web2py.py:

web2py Enterprise Web Framework
Created by Massimo Di Pierro, Copyright 2007-2010
Version 1.81.5 (2010-07-22 23:56:21)
Database drivers available: MySQL
Starting hardcron...
WARNING:root:GUI not available because Tk library is not installed

Thadeus Burgess

unread,
Jul 24, 2010, 12:04:08 PM7/24/10
to web...@googlegroups.com
Perhaps WSGI is configured to use python 2.4 and the libraries are not
linked correctly.

--
Thadeus

mdipierro

unread,
Jul 24, 2010, 12:32:43 PM7/24/10
to web2py-users
So it works when you do

python2.6 web2y.py

but not when you use apache. is that correct?

oktay

unread,
Jul 24, 2010, 12:38:13 PM7/24/10
to web2py-users
Yes, correct.
web2y.py (including "import MySQLdb") works both with python2.4 and
python2.6.
and not working with apache.

Also I make my wsgi configuration as described in
http://web2py.com/book/default/section/11/2

oktay

unread,
Jul 24, 2010, 1:47:19 PM7/24/10
to web2py-users
Hello again,

I installed PostgreSQL and psycopg2.
both in the command line for python24 and python26 can import
psycopg2.

But when I run from apache
"global name 'psycopg2' is not defined"
error appears.

Same problem with mysql driver.

apache/python can not find the python db drivers.
Is it related to wsgi configuration?

Do you have any advice? Where should I check?

Thank you...


On 24 Temmuz, 19:38, oktay <onur.ok...@gmail.com> wrote:
> Yes, correct.
> web2y.py (including "import MySQLdb") works both with python2.4 and
> python2.6.
> and not working with apache.
>
> Also I make my wsgi configuration as described inhttp://web2py.com/book/default/section/11/2

Kenneth

unread,
Jul 24, 2010, 3:51:01 PM7/24/10
to web2py-users
I´m running web2py on CentOS 5.something. If needed I could send you
my Apache configuration files and others.


Kenneth

oktay

unread,
Jul 24, 2010, 3:58:22 PM7/24/10
to web2py-users
I will be very glad if you can...
Thank you.

oktay

unread,
Jul 25, 2010, 4:36:48 AM7/25/10
to web2py-users
I solved the problem.
It is strange but in sql.py before
"self._pool_connection(lambda :MySQLdb.Connection" I add import
MySQLdb.
I print some logs from sql.py and the first import in sql.py is not
processed when running with apache.
I am not sure why first import is not processed but I solved my
problem with adding

"import MySQLdb" before MySQLdb.Connection is processed.

self._pool_connection(lambda : MySQLdb.Connection(
db=db,
user=user,
passwd=passwd,
host=host,
port=int(port),
charset=charset,
))
self._cursor = self._connection.cursor()
self._execute = lambda *a, **b: self._cursor.execute(*a,
**b)
self._execute('SET FOREIGN_KEY_CHECKS=1;')
self._execute("SET sql_mode='NO_BACKSLASH_ESCAPES';")

mdipierro

unread,
Jul 27, 2010, 9:00:15 AM7/27/10
to web2py-users
This is very odd. Can any mod_wsgi expert explain this?

On Jul 25, 3:36 am, oktay <onur.ok...@gmail.com> wrote:
> I solved the problem.
> It is strange but in sql.py before
> "self._pool_connection(lambda :MySQLdb.Connection" I add import
> MySQLdb.
> I print some logs from sql.py and the first import in sql.py is not
> processed when running with apache.
> I am not sure why first import is not processed but I solved my
> problem with adding
>
> "import MySQLdb" before MySQLdb.Connection is processed.
>
>            self._pool_connection(lambda : MySQLdb.Connection(
>                    db=db,
>                    user=user,
>                    passwd=passwd,
>                    host=host,
>                    port=int(port),
>                    charset=charset,
>                    ))
>            self._cursor = self._connection.cursor()
>            self._execute = lambda *a, **b: self._cursor.execute(*a,
> **b)
>            self._execute('SET FOREIGN_KEY_CHECKS=1;')
>            self._execute("SET sql_mode='NO_BACKSLASH_ESCAPES';")
>
> On 24 Temmuz, 22:58,oktay<onur.ok...@gmail.com> wrote:
>
> > I will be very glad if you can...
> > Thank you.
>
> > On 24 Temmuz, 22:51, Kenneth <kenneth.t.lundst...@gmail.com> wrote:
>
> > > I´m running web2py on CentOS 5.something. If needed I could send you
> > > my Apache configuration files and others.
>
> > > Kenneth
>
> > > On Jul 24, 8:47 pm,oktay<onur.ok...@gmail.com> wrote:
>
> > > > Hello again,
>
> > > > I installed PostgreSQL and psycopg2.
> > > > both in the command line for python24 and python26 can import
> > > > psycopg2.
>
> > > > But when I run from apache
> > > > "global name 'psycopg2' is not defined"
> > > > error appears.
>
> > > > Same problem with mysql driver.
>
> > > > apache/python can not find the python db drivers.
> > > > Is it related to wsgi configuration?
>
> > > > Do you have any advice? Where should I check?
>
> > > > Thank you...
>
> > > > On 24 Temmuz, 19:38,oktay<onur.ok...@gmail.com> wrote:
>
> > > > > Yes, correct.
> > > > > web2y.py (including "import MySQLdb") works both with python2.4 and
> > > > > python2.6.
> > > > > and not working with apache.
>
> > > > > Also I make my wsgi configuration as described inhttp://web2py.com/book/default/section/11/2
>
> > > > > On 24 Temmuz, 19:32, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > > > So it works when you do
>
> > > > > > python2.6 web2y.py
>
> > > > > > but not when you use apache. is that correct?
>
> > > > > > On Jul 24, 10:58 am,oktay<onur.ok...@gmail.com> wrote:
>
> > > > > > > Yes, it works with "import MySQLdb" at the top of web2py.py:
>
> > > > > > > web2py Enterprise Web Framework
> > > > > > > Created by Massimo Di Pierro, Copyright 2007-2010
> > > > > > > Version 1.81.5 (2010-07-22 23:56:21)
> > > > > > > Database drivers available: MySQL
> > > > > > > Starting hardcron...
> > > > > > > WARNING:root:GUI not available because Tk library is not installed
>
> > > > > > > On 24 Temmuz, 18:52, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > > > > > As a test, can you try add
>
> > > > > > > >    import MySQLdb
>
> > > > > > > > at the top of web2py.py and run
>
> > > > > > > >    python2.6 web2py.py
>
> > > > > > > > Massimo
>
Reply all
Reply to author
Forward
0 new messages