首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“ModuleNotFoundError(”没有模块名为'MySQLdb'“)”在GCP上部署OpenLiteSpeed django解决方案时

问“ModuleNotFoundError(”没有模块名为'MySQLdb'“)”在GCP上部署OpenLiteSpeed django解决方案时
EN

Stack Overflow用户
提问于 2020-06-15 17:33:22
回答 1查看 159关注 0票数 0

我正在测试Google平台,我在上部署了一个带有Ubuntu的OpenLiteSpeed Django解决方案。当它被部署时,一切都像魅力一样工作,我能够到达"Hello,world“的起始页面。

当我试图添加自己的简单脚本时,将views.py文件在/usr/local/lsws/Example/html/demo/app中更改为:

代码语言:javascript
复制
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时,会遇到以下跟踪和错误:

代码语言:javascript
复制
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'

​

​

我尝试在/usr/bin目录中运行/usr/bin,以及sudo apt install python3-dev和sudo apt install libmysqlclient-dev,但没有成功。有人能帮忙吗?我刚开始处理应用程序。

更新:所以,通过尝试一堆不同的东西,我最终通过cd进入/usr/bin并执行sudo -H ./pip3 install mysqlclient来解决这个问题。我不太清楚为什么在执行./pip3 install mysqlclient时,它没有给我任何权限错误&但是现在这个问题已经解决了。

EN

回答 1

Stack Overflow用户

发布于 2020-06-15 17:50:09

安装了MySQLdb吗?当您输入pip3列表时,会看到它吗?

我在文档中看到,这个建议是像这样导入MySQLdb。

代码语言:javascript
复制
from MySQLdb import _mysql

https://mysqlclient.readthedocs.io/user_guide.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62393747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档