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

slhcrj9b  于 2022-12-14  发布在  Apache
关注(0)|答案(1)|浏览(165)

我正在AWS示例上部署Django应用程序(amazon 2 64位,Linux/4.14.128-112.105.amzn2.x86_64)。我花了很多时间在堆栈溢出中查找和搜索,但没有找到解决方案。
我的问题:正确安装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的帖子,但是没有一个能解决我的问题。

mgdq6dx1

mgdq6dx11#

我遇到了同样的问题。我不确定AWS,但我有问题在我的Mac上。所以发生的是,Mac的安全不允许该设置访问文件MySQLdb._mysql。所以,我只是运行命令sudo cp -r /usr/local/mysql/lib/* /usr/local/lib/和运行命令python manage.py migrate后,它的工作。希望我的答案找到你正在寻找的解决方案。祝你有美好的一天!

相关问题