Django not connecting to MySQL properly, throws "ImportError: cannot import name 'NULL'"
<p>I want to use Django (using Python 3) with MySQL. To do so, I installed <a href="https://pypi.python.org/pypi/mysqlclient" rel="nofollow">mysqlclient</a>. To first test the waters with MySQL, I also created a new Django project and app, changed the DATABASES configuration in "settings.py" 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). </p>

<p>But when I do </p>

<pre><code>python manage.py runserver
</code></pre>

<p>in terminal command, I get the following error:</p>

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

<p>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?</p>

<p>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). </p>

<p>EDIT: I failed to share my DATABASE setting, so here it is. </p>

<pre><code>DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test1',
        'USER': 'admin',
        'PASSWORD': 'dbadmin',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
</code></pre>

<p>Settings.py, 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.</p>

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

<p>Ok, there are several problems here.</p>

<p>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: <a href="http://docs.python-guide.org/en/latest/dev/virtualenvs/" rel="nofollow">http://docs.python-guide.org/en/latest/dev/virtualenvs/</a></p>

<p>but briefly</p>

<pre><code>rm -rf /Users/shtryts/.virtualenvs/django
virtualenv /Users/shtryts/.virtualenvs/myvirtualenv
</code></pre>

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

<p>Then you should install all the requirements with pip such as</p>

<pre><code>pip install django 
</code></pre>

<p>Please note that 'MysqlDB' is not compatible with python 3x please see here for details: <a href="https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-db-api-drivers" rel="nofollow">https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-db-api-drivers</a></p>

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

<pre><code>django-admin starproject myproject
</code></pre>

python mysql django