如何修复ImportError:无法在Django 2.2.5中导入名称'_mysql'

时间:2019-10-01 15:08:21

标签: python django apache amazon-ec2 mod-wsgi

我正在AWS实例(amazon2 64位,Linux / 4.14.128-112.105.amzn2.x86_64)上部署Django应用程序。我花了很多时间在堆栈溢出中查找和搜索,而没有找到解决方案。

我的问题:正确安装mysqlclient。

错误:

mod_wsgi (pid=14418): Failed to exec Python script file '/var/www/html/project_x/project_x/wsgi.py'.
mod_wsgi (pid=14418): Exception occurred processing WSGI script '/var/www/html/project_x/project_x/wsgi.py'.
Traceback (most recent call last):
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
   import MySQLdb as Database
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
   from . import _mysql
ImportError: cannot import name '_mysql'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
 File "/var/www/html/project_x/project_x/wsgi.py", line 17, in <module>
   application = get_wsgi_application()
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
   django.setup(set_prefix=False)
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
   apps.populate(settings.INSTALLED_APPS)
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
   app_config.import_models()
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
   self.models_module = import_module(models_module_name)
 File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
   return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 994, in _gcd_import
 File "<frozen importlib._bootstrap>", line 971, in _find_and_load
 File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 678, in exec_module
 File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
   from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
   class AbstractBaseUser(models.Model):
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/db/models/base.py", line 117, in __new__
   new_class.add_to_class('_meta', Options(meta, app_label))
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/db/models/base.py", line 321, in add_to_class
   value.contribute_to_class(cls, name)
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/db/models/options.py", line 204, in contribute_to_class
   self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
   return getattr(connections[DEFAULT_DB_ALIAS], item)
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/db/utils.py", line 201, in __getitem__
   backend = load_backend(db['ENGINE'])
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
   return import_module('%s.base' % backend_name)
 File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
   return _bootstrap._gcd_import(name[level:], package, level)
 File "/var/www/html/project_x/projectenv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 20, in <module>
   ) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

我已经安装了所有东西,但是没有用。


我在项目文件夹中创建了虚拟环境:

$ virtualenv projectenv --python=python3.7
$ source projectenv/bin/activate

之后,我用pip安装了插件:

$ pip list
Package     Version
----------- -------
Django      2.2.5  
mysqlclient 1.4.4  
pip         19.2.3 
pytz        2019.2 
setuptools  41.2.0 
sqlparse    0.3.0  
wheel       0.33.6 

如您所见,软件包已正确安装。还将安装msqlclient。我不知道该怎么办,我已经咨询了很多有关Stackoverflow的帖子,但是都没有解决我的问题。

0 个答案:

没有答案