简体   繁体   中英

Django error with connect mysql

At first, I try to connect the MySQL in .py, it is successful.

import pymysql
db = pymysql.connect(host='localhost',user='root',passwd='admin',port=3306)
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print ("Database version : %s " % data)
db.close()

But when I use in the Django, is wrong

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'testdb',
        'USER':'root',
        'PASEWORD':'admin',
        'HOST':'localhost',
        'PORT':'3306',
    }
}

----the error:

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

----i add the "import pymysql"in "settings.py"

the error:

ImportError: Could not import settings 'HelloWorld.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named pymysql

version: mac python 3.6 django:1.7.11

It sounds like you are using different virtual environments for your standalone test .py and your Django application; and pymysql is not installed in the Django virtual environment.

in your virtual enviroment install mysqlclient using pip:

pip install mysqlclient

check your python compatibility version here: https://pypi.python.org/pypi/mysqlclient

and specify the version to install like this:

pip install mysqlclient==x.x.x

For python 2.7 you can use 1.3.4 and latest version 1.3.10

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