本文共 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, inimport 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/