博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Problems & Solutions -- 7 MySQLdb in python
阅读量:4099 次
发布时间:2019-05-25

本文共 3248 字,大约阅读时间需要 10 分钟。

学习Django,需要连接Django应用到mysql时,出了问题。

(learn_django) for-python@ubuntu:~/apps/todolist$ python manage.py migrateTraceback (most recent call last):  File "/home/for-python/learn_django/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 26, in 
import MySQLdb as DatabaseImportError: No module named 'MySQLdb'...... 'Did you install mysqlclient or MySQL-python?' % edjango.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.Did you install mysqlclient or MySQL-python?

关于mysqlclient,确认过已经安装。

问题就是MySQLdb模块,或者MySQL-Python 了。

找了很久的解决办法,在MySQLdb官方页面发现了这样一句:

MySQL-3.23 through 5.5 and Python-2.4 through 2.7 are currently supported. Python-3.0 will be supported in a future release.

MySQLdb不支持Python3

而Ubuntu虚拟环境中,默认使用Python3:

(learn_django) for-python@ubuntu:~$ python --versionPython 3.5.3

正常状态下默认使用python2,在命令行中运行python,默认使用python2,能正常导入MySQLdb

(learn_django) for-python@ubuntu:~$ deactivatefor-python@ubuntu:~$ python --versionPython 2.7.13for-python@ubuntu:~$ pythonPython 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import MySQLdb>>>

指定使用Python3,就不能正常导入MySQLdb模块了:

for-python@ubuntu:~$ python3Python 3.5.3 (default, Jan 19 2017, 14:11:04) [GCC 6.3.0 20170118] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import MySQLdbTraceback (most recent call last):  File "
", line 1, in
ImportError: No module named 'MySQLdb'>>>

然后,不使用虚拟环境,直接运行python manage.py migrate ,能正常:

for-python@ubuntu:~/apps/todolist$ python manage.py migrateSystem check identified some issues:WARNINGS:?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.Operations to perform:  Apply all migrations: admin, auth, contenttypes, sessionsRunning 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 sessions.0001_initial... OK

MySQL 数据库中有了相应的记录:

mysql> use todolistReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+----------------------------+| Tables_in_todolist         |+----------------------------+| auth_group                 || auth_group_permissions     || auth_permission            || auth_user                  || auth_user_groups           || auth_user_user_permissions || django_admin_log           || django_content_type        || django_migrations          || django_session             |+----------------------------+10 rows in set (0.00 sec)

转载地址:http://wheii.baihongyu.com/

你可能感兴趣的文章
python九九乘法表(详解)
查看>>
ESP8266 WIFI数传 Pixhaw折腾笔记
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
MongDB学习之路(二)基本指令
查看>>
MongoDB学习之路(三)复制集
查看>>
VMware安装centos6.8常见问题
查看>>
安装git在Linux环境配置
查看>>
【java面试】面向对象三大特性:封装、继承、多态
查看>>
【java面试】equals()与“==”的使用
查看>>
【java面试】静态变量、static关键字及其用法
查看>>
【java并发】AQS学习
查看>>
SpringBoot(一)创建HelloWorld和报错解决
查看>>
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz 下载失败【已解决】
查看>>
Kettle开源项目一款ETL工具
查看>>
kettle学习免费视频教程【正在学习-亲测!】
查看>>
部署Kettle7.1到linux后执行./kitchen.sh报错No libwebkitgtk1.0 detected
查看>>
CentOS7.2的yum、python卸载和重新安装【亲测有效!20190626】
查看>>
给定一个矩阵m*n,从左上角开始每次只能向右或者向下走,最后到右下角的位置共有多少种路径
查看>>