0

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'

enter image description here

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.

Artem
  • 751
  • 2
  • 10
  • 30
  • see here https://stackoverflow.com/questions/47490797/modulenotfounderror-no-module-named-mysqldb-in-django you should switch to aniother driver because this is not supported anymore – nbk Jun 15 '20 at 17:53
  • @nbk when I run python3 directly through SSH in the VM I can `import MySQLdb` with no issues. – Artem Jun 15 '20 at 18:03
  • iI use import mysql.connector that works without a problem, yur umport doesn't work with me and other – nbk Jun 15 '20 at 18:06
  • @nbk That's weird... I'll try switching connector. – Artem Jun 15 '20 at 18:10
  • error fixed by running `sudo -H ./pip3 install mysqlclient` instead of `./pip3 install mysqlclient` – Artem Jun 16 '20 at 05:03

1 Answers1

0

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

DanielHefti
  • 144
  • 1
  • 9