简体   繁体   中英

“ModuleNotFoundError(”No module named 'MySQLdb'“,)” when deploying a OpenLiteSpeed django solution on GCP

I'm testing Google Cloud Platform, where I've deployed a OpenLiteSpeed Django solution with a Ubuntu VM on Google Compute Engine. When it's deployed, everything works like a charm, and I'm able to reach the "Hello, world" starting page.

When I try to add my own simple script by changing the views.py file in /usr/local/lsws/Example/html/demo/app to:

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)

and access the IP again I'm met with the following traceback and error:

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'

在此处输入图像描述

I've tried running ./pip3 install mysqlclient in the /usr/bin directory, along with sudo apt install python3-dev and sudo apt install libmysqlclient-dev with no luck. Can anyone help with this? I'm new working with apps.

UPDATE: So, by trying out a bunch of different stuff, I finally fixed the problem by cd'ing into /usr/bin and executing sudo -H./pip3 install mysqlclient . I'm not entirely sure why it didn't give me any permission error, when just executing ./pip3 install mysqlclient - however now the issue is resolved.

Is MySQLdb installed? Do you see it when you enter pip3 list?

I've seen in the docs that the proposal is to import MySQLdb like this.

from MySQLdb import _mysql

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM