在GCP上部署OpenLiteSpeed Django解决方案时出现“ModuleNotFoundError”(“没有名为'MySQLdb'”的模块)”

2024-06-11 20:27:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试谷歌云平台,我在谷歌计算引擎上部署了一个OpenLiteSpeed Django解决方案和一个Ubuntu虚拟机。当它被部署时,一切都像一个符咒一样工作,我能够到达“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我遇到以下回溯和错误:

Environment:


Request Method: GET
Request URL: http://ip/

Django Version: 3.0.3
Python Version: 3.6.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['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 "<frozen importlib._bootstrap>", line 994, in _gcd_import
    <source code not available>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
    <source code not available>
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
    <source code not available>
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
    <source code not available>
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
    <source code not available>
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    <source code not available>
  File "/usr/local/lsws/Example/html/demo/demo/urls.py", line 18, in <module>
    from app import views
  File "/usr/local/lsws/Example/html/demo/app/views.py", line 1, in <module>
    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但没有成功。有人能帮忙吗?我是一个应用程序新手

更新: 因此,通过尝试一系列不同的东西,我最终通过将cd'ing放入/usr/bin并执行sudo -H ./pip3 install mysqlclient来解决问题。我不完全确定为什么它在执行./pip3 install mysqlclient时没有给我任何权限错误-但是现在问题解决了


Tags: djangoinpyimportselfexamplelibusr