PyMySQL Python 3 Django VirtualEnv MySQLdb
<p>So I had this running yesterday when I was running the virtualenv out of my home directory.  I've ran in to this problem a number of times now without a clear answer.  I've done a lot of research on this but there seems to be no black and white answer.  Today I moved my virtualenv to <code>/usr/local/virtualenvs</code> to better manage my projects and viola, it's broken again.  <code>Error loading MySQLdb module: No module named 'MySQLdb'</code> is the bane of my existence right now.  I can't seem to fix it this time.  I have even tried duplicating what I did back into my home directory and have the very same problem.  I haven't deleted any packages from my system.  I installed the very same packages in the virtualenv.  At this point I'm lost.  I've even written down the steps I took <em>from a fresh install</em> to get to a working virtualenv.  I certainly don't want to go back to a fresh install.  Please help :)</p>

<p><strong>Virtual Environment:</strong></p>

<pre><code>Ubuntu Server 16.04 LTS
python 3.5
django 1.10.2
pymysql 0.7.9
</code></pre>

<p><strong>My settings.py</strong></p>

<pre><code>DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'auth',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'localhost',
        'PORT': '3306',
    },
    'cogs': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'cogs',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
</code></pre>

<p><strong>Error:</strong></p>

<pre><code>Traceback (most recent call last):
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 25, in &lt;module&gt;
    import MySQLdb as Database
ImportError: No module named 'MySQLdb'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./manage.py", line 10, in &lt;module&gt;
    execute_from_command_line(sys.argv)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/virtualenvs/rtservice/serviceenv/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 "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in &lt;module&gt;
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 52, in &lt;module&gt;
    class AbstractBaseUser(models.Model):
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/models/base.py", line 119, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/models/base.py", line 316, in add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/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 "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/utils.py", line 211, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 28, in &lt;module&gt;
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
</code></pre>

<p>Thanks in advance.</p>

<p>Added</p>

<pre><code>try:
    import pymysql
    pymysql.install_as_MySQLdb()
except ImportError:
    pass
</code></pre>

<p>to <code>manage.py</code> and <code>wsgi.py</code></p>

<p>Now working.</p>

python mysql django virtualenv