python如果要使用mysql,需要第三方的模块MYSQL-python,但是使用pip install安装的时候很可能会遇到如下的问题,我的python版本是3.6.0,问题如下:
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
按照这个提示,去下载Microsoft Visual Studio 14.0,下载安装后又提示:
_mysql.c(42): fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2
简直是崩溃了,windows下使用python确实会出现比linux多的多的问题
在网上找了很多办法,最后发现一个解决方法,去如下网址下载对应的驱动https://pypi.python.org/pypi/mysqlclient/1.3.10
将下载的文件放到pip.exe同目录下,然后执行pip install mysqlclient-1.3.10-cp36-cp36m-win_amd64.whl
>pip install mysqlclient-1.3.10-cp36-cp36m-win_amd64.whl
Processing c:\python\python36\scripts\mysqlclient-1.3.10-cp36-cp36m-win_amd64.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.10
检验一下mysql驱动模块是否安装成功
λ python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
没有报错,应该是安装成功了
我又在django的工程下验证了一下
首先需要在本地的mysql数据库中创建一个database:
create database mysite;
需要在我的工程下的settings.py中配置本地mysql的数据源
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite',
'USER': 'root',
'PASSWORD': '123123',
'HOST': '127.0.0.1', # 默认
'PORT': '3306', # 默认
}
}
然后执行命令
λ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
指令运行完后,mysql数据库中会自动创建一些django自带的表
问题解决!