title: Python NameError: name '_mysql' is not defined
body: <p>I am trying to have a working ubuntu server with Python, Django and MySQL. But when I switch from SQLite to MySQL and trying to reach my server I get the following error &quot;NameError: name '_mysql' is not defined&quot;.</p>
  <p>It does work if I run the project in virtual environment mode with the command &quot;python manage.py runserver&quot;. The database is also correctly created with the command &quot;python manage.py migrate&quot;. This does not work when I try to reach my URL. I think there is an issue with my virtual host file, but I am unable to see it. Any help would be appreciated!</p>
  <p><strong>Virtual host file:</strong></p>
  <pre><code>&lt;VirtualHost *:80&gt;
      ServerName mysite.com
      ServerAlias www.mysite.com
  
      WSGIDaemonProcess myprocess python-path=/var/www/myproject/myname:/var/www/myproject/myname/venv/lib/python3.7/site-packages 
      WSGIProcessGroup myprocess    
       
      WSGIScriptAlias / /var/www/myproject/myname/myproject/wsgi.py
  
      &lt;Location / &gt;
        Order deny,allow
        Deny from all
        # ek
        Allow from ${IP_USER1}
        Allow from ${IP_USER2}
      &lt;/Location&gt;
  
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
  &lt;/VirtualHost&gt;
  </code></pre>
  <p><strong>The error log:</strong></p>
  <pre><code>    mod_wsgi (pid=55852): Failed to exec Python script file '/var/www/myproject/wsgi.py'.
      mod_wsgi (pid=55852): Exception occurred processing WSGI script '/var/www/myproject/myname/myproject/wsgi.py'.
  
      Traceback (most recent call last):
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/MySQLdb/__init__.py&quot;, line 18, in &lt;module&gt;
      from . import _mysql
      ImportError: cannot import name '_mysql' from partially initialized module 'MySQLdb' (most likely due to a circular import) (/var/www/myproject/myname/venv/lib/python3.7/site-packages/MySQLdb/__init__.py)
  
      During handling of the above exception, another exception occurred:
      Traceback (most recent call last):
      File &quot;/var/www/myproject/myname/myproject/wsgi.py&quot;, line 16, in &lt;module&gt;
      application = get_wsgi_application()
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/core/wsgi.py&quot;, line 12, in get_wsgi_application
      django.setup(set_prefix=False)
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/__init__.py&quot;, line 24, in setup
      apps.populate(settings.INSTALLED_APPS)
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/apps/registry.py&quot;, line 115, in populate
      app_config.import_models()
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/apps/config.py&quot;, line 211, in import_models
      self.models_module = import_module(models_module_name)
      File &quot;/usr/lib/python3.9/importlib/__init__.py&quot;, line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
      File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1030, in _gcd_import
      File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1007, in _find_and_load
      File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 986, in _find_and_load_unlocked
      File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 680, in _load_unlocked
      File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 850, in exec_module
      File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 228, in _call_with_frames_removed
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/contrib/auth/models.py&quot;, line 2, in &lt;module&gt;
      from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/contrib/auth/base_user.py&quot;, line 47, in &lt;module&gt;
      class AbstractBaseUser(models.Model):
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/models/base.py&quot;, line 121, in __new__
      new_class.add_to_class('_meta', Options(meta, app_label))
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/models/base.py&quot;, line 325, in add_to_class
      value.contribute_to_class(cls, name)
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/models/options.py&quot;, line 208, in contribute_to_class
      self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/__init__.py&quot;, line 28, in __getattr__
      return getattr(connections[DEFAULT_DB_ALIAS], item)
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/utils.py&quot;, line 207, in __getitem__
      backend = load_backend(db['ENGINE'])
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/utils.py&quot;, line 111, in load_backend
      return import_module('%s.base' % backend_name)
      File &quot;/usr/lib/python3.9/importlib/__init__.py&quot;, line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py&quot;, line 16, in &lt;module&gt;
      import MySQLdb as Database
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/MySQLdb/__init__.py&quot;, line 24, in &lt;module&gt;
      version_info, _mysql.version_info, _mysql.__file__
      NameError: name '_mysql' is not defined
      mod_wsgi (pid=55852): Failed to exec Python script file '/var/www/myproject/myname/myproject/wsgi.py'.
      mod_wsgi (pid=55852): Exception occurred processing WSGI script '/var/www/myproject/myname/myproject/wsgi.py'.
  
      Traceback (most recent call last):
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/MySQLdb/__init__.py&quot;, line 18, in &lt;module&gt;
      from . import _mysql
      ImportError: cannot import name '_mysql' from partially initialized module 'MySQLdb' (most likely due to a circular import) (/var/www/myproject/myname/venv/lib/python3.7/site-packages/MySQLdb/__init__.py)
  
      During handling of the above exception, another exception occurred:
      Traceback (most recent call last):
      File &quot;/var/www/myproject/myname/myproject/wsgi.py&quot;, line 16, in &lt;module&gt;
      application = get_wsgi_application()
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/core/wsgi.py&quot;, line 12, in get_wsgi_application
      django.setup(set_prefix=False)
       File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/__init__.py&quot;, line 24, in setup
      apps.populate(settings.INSTALLED_APPS)
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/apps/registry.py&quot;, line 115, in populate
      app_config.import_models()
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/apps/config.py&quot;, line 211, in import_models
      self.models_module = import_module(models_module_name)
      File &quot;/usr/lib/python3.9/importlib/__init__.py&quot;, line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
      File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1030, in _gcd_import
      File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1007, in _find_and_load
      File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 986, in _find_and_load_unlocked
      File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 680, in _load_unlocked
      File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 850, in exec_module
      File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 228, in _call_with_frames_removed
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/contrib/auth/models.py&quot;, line 2, in &lt;module&gt;
      from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/contrib/auth/base_user.py&quot;, line 47, in &lt;module&gt;
      class AbstractBaseUser(models.Model):
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/models/base.py&quot;, line 121, in __new__
      new_class.add_to_class('_meta', Options(meta, app_label))
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/models/base.py&quot;, line 325, in add_to_class
      value.contribute_to_class(cls, name)
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/models/options.py&quot;, line 208, in contribute_to_class
      self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/__init__.py&quot;, line 28, in __getattr__
      return getattr(connections[DEFAULT_DB_ALIAS], item)
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/utils.py&quot;, line 207, in __getitem__
      backend = load_backend(db['ENGINE'])
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/utils.py&quot;, line 111, in load_backend
      return import_module('%s.base' % backend_name)
      File &quot;/usr/lib/python3.9/importlib/__init__.py&quot;, line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py&quot;, line 16, in &lt;module&gt;
      import MySQLdb as Database
      File &quot;/var/www/myproject/myname/venv/lib/python3.7/site-packages/MySQLdb/__init__.py&quot;, line 24, in &lt;module&gt;
      version_info, _mysql.version_info, _mysql.__file__
      NameError: name '_mysql' is not defined
  </code></pre>
  <p><strong>The wsgi.py file:</strong></p>
  <pre><code>&quot;&quot;&quot;
  WSGI config for myproject project.
  
  It exposes the WSGI callable as a module-level variable named ``application``.
  
  For more information on this file, see
  https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
  &quot;&quot;&quot;
  
  import os
  
  from django.core.wsgi import get_wsgi_application
  
  os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
  
  application = get_wsgi_application()
  </code></pre>
  <p><strong>The requirements.txt file:</strong></p>
  <pre><code>asgiref==3.5.2
  Django==3.0
  mysqlclient==2.1.1
  pytz==2022.1
  sqlparse==0.4.2
  typing_extensions==4.2.0
  </code></pre>