在GCP部署OpenLiteSpeed django解决方案时,“ModuleNotFoundError(“No module named 'MySQLdb'”)”错误。

7 浏览
0 Comments

在GCP部署OpenLiteSpeed django解决方案时,“ModuleNotFoundError(“No module named 'MySQLdb'”)”错误。

我正在测试Google Cloud Platform,我在Google Compute Engine上部署了一个使用Ubuntu虚拟机的OpenLiteSpeed Django解决方案。当部署完成后,一切都运行得很顺利,我可以访问到“Hello, world”的起始页面。

当我尝试通过修改/usr/local/lsws/Example/html/demo/app中的views.py文件来添加自己的简单脚本时:

import MySQLdb
from django.shortcuts import render
from django.http import HttpResponse
def getFromDB():
    data = []
    db = MySQLdb.connect(host="ip",
                      user="user",
                      passwd="pw",
                      db="db")
    cur = db.cursor()
    cur.execute("SELECT * FROM table")
    for student in students:
        data.append(student)
    return data
def index(request):
    return HttpResponse(getFromDB)

并且再次访问IP时,我遇到了以下的追踪和错误信息:

环境:
请求方式:GET
请求URL:http://ip/
Django版本:3.0.3
Python版本:3.6.9
已安装的应用程序:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
已安装的中间件:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
  File "/usr/local/lsws/Example/html/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lsws/Example/html/lib/python3.6/site-packages/django/core/handlers/base.py", line 100, in _get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/usr/local/lsws/Example/html/lib/python3.6/site-packages/django/urls/resolvers.py", line 544, in resolve
    for pattern in self.url_patterns:
  File "/usr/local/lsws/Example/html/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lsws/Example/html/lib/python3.6/site-packages/django/urls/resolvers.py", line 588, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lsws/Example/html/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lsws/Example/html/lib/python3.6/site-packages/django/urls/resolvers.py", line 581, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/local/lsws/Example/html/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 994, in _gcd_import
    
  File "", line 971, in _find_and_load
    
  File "", line 955, in _find_and_load_unlocked
    
  File "", line 665, in _load_unlocked
    
  File "", line 678, in exec_module
    
  File "", line 219, in _call_with_frames_removed
    
  File "/usr/local/lsws/Example/html/demo/demo/urls.py", line 18, in 
    from app import views
  File "/usr/local/lsws/Example/html/demo/app/views.py", line 1, in 
    import MySQLdb
Exception Type: ModuleNotFoundError at /
Exception Value: No module named 'MySQLdb'

enter image description here

我尝试在/usr/bin目录下运行./pip3 install mysqlclient,以及运行sudo apt install python3-devsudo apt install libmysqlclient-dev,但都没有成功。有人能帮忙吗?我在处理应用程序方面还是新手。

更新:

所以,通过尝试了各种不同的方法,我最终通过进入/usr/bin目录并执行sudo -H ./pip3 install mysqlclient来解决了这个问题。我不太确定为什么执行./pip3 install mysqlclient时没有给我任何权限错误,但现在问题已经解决了。

0
0 Comments

在部署Google Cloud Platform(GCP)上的OpenLiteSpeed Django解决方案时,出现了"ModuleNotFoundError("No module named 'MySQLdb'",)"的错误。

该错误的原因是MySQLdb模块未安装。通过输入pip3 list命令,可以查看已安装的模块列表,检查是否存在MySQLdb模块。

解决方法是根据文档中的建议,使用以下方式导入MySQLdb模块:

from MySQLdb import _mysql

详情请参考:https://mysqlclient.readthedocs.io/user_guide.html

尝试更改脚本中的导入方式,但未能解决问题。

确保已正确安装MySQLdb模块。

0