Traceback (most recent call last):
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 15, in
import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in
execute_from_command_line(sys.argv)
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "" , line 994, in _gcd_import
File "" , line 971, in _find_and_load
File "" , line 955, in _find_and_load_unlocked
File "" , line 665, in _load_unlocked
File "" , line 678, in exec_module
File "" , line 219, in _call_with_frames_removed
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in
class AbstractBaseUser(models.Model):
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/models/base.py", line 114, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/models/base.py", line 315, in add_to_class
value.contribute_to_class(cls, name)
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/models/options.py", line 205, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/utils.py", line 202, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 20, in
) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
Django安装完默认的数据库驱动是sqlite,而我并没有sqlite数据库,那就改成
mysql吧,于是进入目录first/first/settings.py中,找到:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
修改成:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'first',
'USER': 'python',
'PASSWORD': '123456',
'HOST': '192.168.99.124',
# 'PORT': '',
}
}
运行命令 python manage.py runserver
启动服务报如下错误:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
很显然,没有装mysql驱动嘛,装一下就行了呗,于是乎我在命令行快速的打出了命令:
pip install mysql-python
下载完在安装的时候又报了一个错:ImportError: No module named ‘ConfigParser’
原因:Python3.x相对于python2.x中的ConfigParser全部改成小写了,说明pip中的mysql-python不支持python3.x呗,可能是pip中更新的比较慢吧,那我们就去下载他们的源码安装,然而他的源码最新更新时间是2012年,依然没有对python3.x支持!
这可如何是好?换个支持python3的驱动试试吧,心中万马奔腾,当然是草泥马,于是乎在github上一顿搜索,找到了一个PyMySQL(https://github.com/PyMySQL/PyMySQL ),当然pip中也有它,于是我又飞快的在命令行中打出了:
pip install PyMySQL
非常顺利的就安装成功了,然而Django并不认这个外来的和尚,咋办呢,也好办,找到mysite/mysite/init.py,在里面输入以下内容并保存:
import pymysql
pymysql.install_as_MySQLdb()
然后我再运行python manage.py runserver时,又爆了一个提示:
You have unapplied migrations; your app may not work properly until they are applied. Run ‘python manage.py migrate’ to apply them.
当然这个提示并不影响自带服务器的运行,这时候我们访问http://127.0.0.1:8000,会看到成功提示:
It worked!
Congratulations on your first Django-powered page.
Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [app_label].
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!
当然了,对于前面那个警告提示,我当然看着不爽,而且他也告诉我怎么做可以解决他,当然要处理啦!我飞快的复制一下内容到命令行中:
python manage.py makemigrations
python manage.py migrate
然后在重启服务,就很正常啦!