Shotaro Yatsu Shotaro Yatsu - 1 year ago 72
MySQL Question

Django not connecting to MySQL properly, throws "ImportError: cannot import name 'NULL'"

I want to use Django (using Python 3) with MySQL. To do so, I installed mysqlclient. To first test the waters with MySQL, I also created a new Django project and app, changed the DATABASES configuration in "" to settings I prepared in MySQL, and ensured that MySQL server is running (the database I created for testing does not have any tables within it at this point).

But when I do

python runserver

in terminal command, I get the following error:

ImportError: No module named '_mysql_exceptions'
ImportError: No module named '_mysql_exceptions'
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x103efaea0>
Traceback (most recent call last):
File "/Users/shtryts/Django-1.10.1/django/utils/", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/shtryts/Django-1.10.1/django/core/management/commands/", line 113, in inner_run
File "/Users/shtryts/Django-1.10.1/django/utils/", line 249, in raise_last_exception
File "/Users/shtryts/Django-1.10.1/django/utils/", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/shtryts/Django-1.10.1/django/utils/", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/shtryts/Django-1.10.1/django/", line 27, in setup
File "/Users/shtryts/Django-1.10.1/django/apps/", line 108, in populate
File "/Users/shtryts/Django-1.10.1/django/apps/", line 199, in import_models
self.models_module = import_module(models_module_name)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/shtryts/Django-1.10.1/django/contrib/auth/", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/shtryts/Django-1.10.1/django/contrib/auth/", line 52, in <module>
class AbstractBaseUser(models.Model):
File "/Users/shtryts/Django-1.10.1/django/db/models/", line 119, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Users/shtryts/Django-1.10.1/django/db/models/", line 316, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/shtryts/Django-1.10.1/django/db/models/", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/shtryts/Django-1.10.1/django/db/", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Users/shtryts/Django-1.10.1/django/db/", line 211, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/shtryts/Django-1.10.1/django/db/", line 115, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/Users/shtryts/Django-1.10.1/django/db/backends/mysql/", line 31, in <module>
from MySQLdb.converters import Thing2Literal, conversions # isort:skip
File "/Users/shtryts/.virtualenvs/django/lib/python3.5/site-packages/MySQLdb/", line 35, in <module>
from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
ImportError: cannot import name 'NULL'

Should I be modifying a .py file within MySQLdb folder? If so, what should I do so that the Django project properly connects with the database?

I'm testing in a virtualenv, Mac OS X 10.10.3. Thanks in advance for the help. (Also, I did not find a similar question as mine, but apologies if this has been addressed here before).

EDIT: I failed to share my DATABASE setting, so here it is.

'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test1',
'USER': 'admin',
'PASSWORD': 'dbadmin',
'HOST': 'localhost',
'PORT': '3306',
}, the django project I created for testing purposes, mysqlclient installs, django framework, they're all inside virtualenv. Originally, mysqlclient (includes MySQLdb) was installed via "pip install" under the path /Library/Frameworks/Python.framework/Versions/3.5... I made a copy of the installed folders and placed them under the python site-package folder within virtualenv. From what I understand Django does not have a problem locating those folders and files.

which pip output: /Users/shtryts/.virtualenvs/django/bin/pip. And my own code is in .virtualenvs/django/test/mySQLtest

Answer Source

Ok, there are several problems here.

First of all you cannot copy files from one python installation or virtual installation to another python installation or virtualenv. Please delete your current virtualenv and make a new one, detailed instructions are here:

but briefly

rm -rf /Users/shtryts/.virtualenvs/django
virtualenv /Users/shtryts/.virtualenvs/myvirtualenv

I recommend not using django as the name of the virtualenv. Use a name that shows which version of python is being used. Now activate the virtualenv before going to the next step.

Then you should install all the requirements with pip such as

pip install django 

Please note that 'MysqlDB' is not compatible with python 3x please see here for details:

Last but not least. one does not put one's own code inisde the django installation folders. instead in your home or workspace

django-admin starproject myproject