Django

Code

Ticket #5531 (closed: fixed)

Opened 1 year ago

Last modified 3 months ago

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

Reported by: anonymous Assigned to: ramiro
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords: mysql
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

t5531.diff (0.7 kB) - added by ramiro on 09/28/07 08:18:59.
Reuploading the same patch file because the Trac attachments missing problem after the django server move

Change History

09/18/07 04:24:03 changed by anonymous

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

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

(follow-up: ↓ 6 ) 09/18/07 05:51:53 changed by ramiro

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

diff -r 32a3fe78cb9b django/db/backends/mysql/base.py
--- a/django/db/backends/mysql/base.py  Mon Sep 17 11:48:33 2007 -0300
+++ b/django/db/backends/mysql/base.py  Tue Sep 18 07:50:05 2007 -0300
@@ -17,7 +17,8 @@ version = Database.version_info
 version = Database.version_info
 if (version < (1,2,1) or (version[:3] == (1, 2, 1) and
         (len(version) < 5 or version[3] != 'final' or version[4] < 2))):
-    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__)
 
 from MySQLdb.converters import conversions
 from 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)

09/18/07 06:23:00 changed by anonymous

  • has_patch set to 1.
  • summary changed from ImportError: No module named mysql.base to properly report when the mysqldb python package doesn't meet the version requirements.
  • stage changed from Unreviewed to Ready for checkin.

tested, works as expected.

09/18/07 06:26:38 changed by PhiR

  • status changed from new to closed.
  • has_patch deleted.
  • resolution set to invalid.

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

09/18/07 06:51:17 changed by ramiro

  • status changed from closed to reopened.
  • resolution deleted.

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").

(in reply to: ↑ 2 ) 09/19/07 03:09:16 changed 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)

09/23/07 04:01:00 changed by ubernostrum

  • stage changed from Ready for checkin to Accepted.

Please don't anonymously mark things ready for checkin.

09/28/07 08:18:59 changed by ramiro

  • attachment t5531.diff added.

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

09/28/07 08:23:39 changed by ramiro

  • has_patch set to 1.

12/02/07 13:31:19 changed by ramiro

  • owner changed from nobody to ramiro.
  • status changed from reopened to new.

03/21/08 10:00:22 changed by PhiR

  • stage changed from Accepted to Ready for checkin.

works as expected. Tested here.

03/24/08 09:02:44 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(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.

08/20/08 13:04:43 changed by pixelcort

  • status changed from closed to reopened.
  • resolution deleted.

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.

08/20/08 14:55:20 changed by mir

  • has_patch deleted.
  • stage changed from Ready for checkin to Unreviewed.

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

08/20/08 19:18:08 changed 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

08/20/08 19:39:02 changed by mtredinnick

  • status changed from reopened to closed.
  • resolution set to fixed.

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.


Add/Change #5531 (properly report when the mysqldb python package doesn't meet the version requirements)




Change Properties
Action