Opened 8 years ago

Closed 7 years ago

#5531 closed (fixed)

properly report when the mysqldb python package doesn't meet the version requirements

Reported by: anonymous Owned by: ramiro
Component: Database layer (models, ORM) Version: master
Severity: Keywords: mysql
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I got this error:

Traceback (most recent call last):
  File "low_level_test1.cgi", line 84, in ?
    from django.test.utils import setup_test_environment
  File "./django/test/__init__.py", line 6, in ?
    from django.test.testcases import TestCase
  File "./django/test/testcases.py", line 6, in ?
    from django.db import transaction
  File "./django/db/__init__.py", line 23, in ?
    backend = __import__('%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
ImportError: No module named mysql.base

But this is not the true error. The real problem is the version checking routines here: http://code.djangoproject.com/browser/django/trunk/django/db/backends/mysql/base.py#L13
Is raises a ImportError, too!

So i don't see message "MySQLdb-1.2.1p2 or newer is required; you have..." i see the wrong error messages from above: "No module named mysql.base"

Solution: I think the Version Exception must be a different class and not the generetic ImportError.

Attachments (1)

t5531.diff (763 bytes) - added by ramiro 8 years ago.
Reuploading the same patch file because the Trac attachments missing problem after the django server move

Download all attachments as: .zip

Change History (16)

comment:1 Changed 8 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Note: I have MySQLdb v1.2.1g2 and not v1.2.1p2. Is there a different?

comment:2 follow-up: Changed 8 years ago by ramiro

Can you try changing the exception thwon on line 20 from ImportError to ImproperlyConfigured applying this mini-patch?:

  • django/db/backends/mysql/base.py

    diff -r 32a3fe78cb9b django/db/backends/mysql/base.py
    a b version = Database.version_info 
    1717version = Database.version_info
    1818if (version < (1,2,1) or (version[:3] == (1, 2, 1) and
    1919        (len(version) < 5 or version[3] != 'final' or version[4] < 2))):
    20     raise ImportError("MySQLdb-1.2.1p2 or newer is required; you have %s" % Database.__version__)
     20    from django.core.exceptions import ImproperlyConfigured
     21    raise ImproperlyConfigured("MySQLdb-1.2.1p2 or newer is required; you have %s" % Database.__version__)
    2122
    2223from MySQLdb.converters import conversions
    2324from MySQLdb.constants import FIELD_TYPE

(and yes, anything older than MySQLdb 1.2.1p1 won't work:

http://www.djangoproject.com/documentation/databases/#mysqldb

http://code.djangoproject.com/changeset/4751

Upgrade to a non-gamma version of MySQLdb)

comment:3 Changed 8 years ago by anonymous

  • Has patch set
  • Summary changed from ImportError: No module named mysql.base to properly report when the mysqldb python package doesn't meet the version requirements
  • Triage Stage changed from Unreviewed to Ready for checkin

tested, works as expected.

comment:4 Changed 8 years ago by PhiR

  • Has patch unset
  • Resolution set to invalid
  • Status changed from new to closed

The OP didn't see the version import error, he really doesn't have a mysql db module.

comment:5 Changed 8 years ago by ramiro

  • Resolution invalid deleted
  • Status changed from closed to reopened

The OP is reporting that raising ImportError is confusing because it generates a misleading
"import error no module mysql.base" insted of reporting the unmet MysQLDb version requeriment.

Here I have a valid environment (ok, actually without a mysql server :) created by startproject/startapp and I'm algo getting "ImportError: No module named mysql.base" when simulating the reported scenario and running manage.py syncdb

Reopening the ticket and posting the proposed fix as a patch (it changes the error report to "django.core.exceptions.ImproperlyConfigured: MySQLdb-1.2.1p2 or newer is required; you have x.x.x").

comment:6 in reply to: ↑ 2 Changed 8 years ago by anonymous

Replying to ramiro:

 -    raise ImportError("MySQLdb-1.2.1p2 or newer is required; you have %s" % Database.__version__)
 +    from django.core.exceptions import ImproperlyConfigured
 +    raise ImproperlyConfigured("MySQLdb-1.2.1p2 or newer is required; you have %s" % Database.__version__)

Hm. Use ImproperlyConfigured should be a good solution, i think.

(Btw. Attachment t5531.diff not found)

comment:7 Changed 8 years ago by ubernostrum

  • Triage Stage changed from Ready for checkin to Accepted

Please don't anonymously mark things ready for checkin.

Changed 8 years ago by ramiro

Reuploading the same patch file because the Trac attachments missing problem after the django server move

comment:8 Changed 8 years ago by ramiro

  • Has patch set

comment:9 Changed 8 years ago by ramiro

  • Owner changed from nobody to ramiro
  • Status changed from reopened to new

comment:10 Changed 7 years ago by PhiR

  • Triage Stage changed from Accepted to Ready for checkin

works as expected. Tested here.

comment:11 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(In [7358]) Fixed #5531 -- Changes a while back meant we are handling import errors from
database backends differently now. Which meant the MySQLdb version check was
being swallowed. Changed the exception type to ensure this is percolated
correctly. Patch from Ramiro Morales.

comment:12 Changed 7 years ago by pixelcort

  • Resolution fixed deleted
  • Status changed from closed to reopened

django SVN r8449. Created a project and set project's settings.py's DATABASE_ENGINE to mysql and set all other options. Running "python manage.py syncdb" causes error "ImportError: No module named mysql.base". Mac OS X Leopard Server 10.5.4. Python 2.5.1.

comment:13 Changed 7 years ago by mir

  • Has patch unset
  • Triage Stage changed from Ready for checkin to Unreviewed

Is MySQLdb installed? Which version? Can you "import MySQLdb"?

comment:14 Changed 7 years ago by pixelcort

rtdevmini:~ admin$ python
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

import MySQLdb

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ImportError: No module named MySQLdb

comment:15 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from reopened to closed

In that case, your problem has nothing to do with this bug, which was about checking the version when !MySQL was available and that was addressed in the above commit. You have a configuration problem. The django-users mailing list will be a better place to get help with that problem.

Note: See TracTickets for help on using tickets.
Back to Top