Forums

No module named 'MySQLdb'

Hi everybody,

I created a virtualenv with python 3.3 and django 1.6. I set the settings.py with my default database info:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'danbabelnick$default',
        'USER': 'danbabelnick',
        'PASSWORD': 'mypassword',
        'HOST': 'mysql.server',
        'PORT': '3306',
    }
}

But, when I try to execute the first syncdb command:

python manage.py syncdb

I got the following error:

Traceback (most recent call last):
  File "/home/danbabelnick/envs/babyboxenv/lib/python3.3/site-packages/django/db/backends/mysql/base.py", line 14, in <module>
    import MySQLdb as Database
ImportError: No module named 'MySQLdb'

.............
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

Any help please?

For Python 3, you have to install "MySQL-for-Python-3":

Open a Bash console, then:

pip3.3 install --user https://github.com/davispuh/MySQL-for-Python-3/archive/1.0.tar.gz

from the PA Wiki at:

https://www.pythonanywhere.com/wiki/UsingMySQL

HTH Jim

what Jim said. thanks Jim.

Thank you very much!

Update: the correct pip package for Python 3.x has changed since the last update on this thread. If you're using a virtualenv with Python 3, you can install it like this:

pip install mysqlclient

If you're not using a virtualenv, you don't need to do anything -- it's already installed.

Hopefully this thread is still alive...I am not using a virtuelenv and 'import mysqldb' is not working in the first Flask blueprint file accessed, giving an error 'no module named mysqldb'. I tried 'import mysqldb as database' with same error. 'import mysql-connector' also gives same error. Any help would be appreciated... Donald

check what version of python you are running, and go here to check if mysqldb is a package that is automatically installed? if it is not installed, or you are in fact running inside of a virtualenv (just double check), then you would have to install it yourself like this.

Thx Conrad. I am running Python 3.8 and the 'batteries included' page shows that mysql.connector is supported for this version in non virtual environments. But I still get 'no module named mysql' when it encounters 'import mysql.connector'.

how are you running this? eg: from a console? from a file? from a webapp?

from a Chrome browser calling 'https://condofix.pythonanywhere.com/'

*what is the command you are running, and where are you running it? or is this your webapp code throwing an error?

it's my webapp code that's giving the error. Here's the error log info: Error running WSGI application 2021-04-06 17:25:01,204: ModuleNotFoundError: No module named 'mysql' 2021-04-06 17:25:01,205: File "/var/www/condofix_pythonanywhere_com_wsgi.py"

Do you use a virtual environment to run your web app?

Hi, no virtual environment. The app was running fine on PythonAnywhere when I was using SQlite3. Stopped working when I migrated to mySQL.

BTW, the mySQL app is working well on my local machine.

What I can see in your web app setup is that you are using a virtual env. It looks like mysql is not installed in it, could you check that?

How can I be using a virtual env if I never performed any of the steps outlined in your help page (https://help.pythonanywhere.com/pages/Virtualenvs/)? My previous app was running fine with Import SQLite3.

I ran this code in my flask_app.py file to check for virtual environment:

import os if os.getenv('VIRTUAL_ENV'): print('Using Virtualenv') else: print('Not using Virtualenv')

In server log, indicated 'Not using Virtualenv'

however, I just saw that the following statement is also in the server log: 'Set PythonHome to /home/CondoFix/.virtualenvs/myvirtualenv'. Interesting: it has been in the server log since the beginning when the app was working well with SQLite instead of mySQL...

I also installed mySQL connector with the bash console in the virtual environment. No change, app still showing same error: 'no module named mySQL'

Did you reload the site (using the green button on the "Web" page) after installing MySQL Connector? Your site was just automatically reloaded by our system, and it looks like the error message at the bottom of the error log has now changed -- it looks like you need to install pytz into your virtualenv too.

Installed pytz and all is well! Many thanks to the support team :) Donald

Excellent, glad we could help!