pip3 mysql,Django:如何使用pip3安装mysql / connector python

I am working on projects based on Django 1.7 and Python 3.4. However, I had problems installing MySQL/Connector Python with pip3.

According to this document, MySQL/Connector Python supports Python 3. I used to install MySQL-python in Python with command pip install MySQL-python.

This download page only provides .deb files for installation on Ubuntu (btw, the installation also has conflict problems)

I tried to install with:

pip3 install mysql-connector-python --allow-external mysql-connector-python

No error messages. But when I run the Django app, I got the following error message:

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

Question:

So, how do I install MySQL/Connector Python into a virtual environment with pip3? Or is it supposed to be installed into the system, instead of a virtual environment?

解决方案

If you read the documentation, you will see that the native MySQLdb driver doesn't support Python 3. You have two options:

A mysqldb fork

There is a fork that supports Python 3. Read its Github repo to know how to install with your system. For Ubuntu do apt-get install python-mysqldb

MySQL connector

Install from venv as you did with pip install mysql-connector-python --allow-external mysql-connector-python. Then read their documentation for Django and modify your settings.py file to have it like:

DATABASES = {

'default': {

'NAME': 'user_data',

'ENGINE': 'mysql.connector.django',

'USER': 'mysql_user',

'PASSWORD': 'priv4te',

'OPTIONS': {

'autocommit': True,

},

}

}

Pay attention to the connector value mysql.connector.django :-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值