解决Django 配置mysql 遇到 No module named MySQLdb or MySQL-python
防止环境变量带来的一系列问题,先安装python 虚拟环境:pip install virtualenv
具体可浏览 http://www.cnblogs.com/technologylife/p/6635631.html1.创建项目虚拟环境 :$ virtualenv Webvenv
2.指定python 解释器版本:$ virtualenv -p /usr/include/python2.7/bin/python2.7 Webvenv [路径为你的python的具体安装路径]
3.启动虚拟环境:$ source Webvenv/bin/activate
[root@python2 /]# source /Webvenv/bin/activate
(Webvenv) [root@python2 /]# ls
4.pip 安装自己需要的modules 并 创建工程
(Webvenv) [root@python2 webtest]# django-admin startproject WT
(Webvenv) [root@python2 webtest]# ls
WT
(Webvenv) [root@python2 webtest]# cd WT/
(Webvenv) [root@python2 WT]# ls
manage.py WT
5.更改数据库配置:$ vi setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'WT',
'USER': 'root',
'PASSWORD': '111111',
'HOST': '192.168.xxx.xxx',
'PORT': '3306',
}
}
6.修改models: $vi models.py
(Webvenv) [root@python2 WTapp]# vi models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Employee(models.Model):
name=models.CharField(max_length=20)
7. $ ./manage.py 报错:
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Webvenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Webvenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 337, in execute
django.setup()
File "/Webvenv/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Webvenv/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/Webvenv/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/include/python2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Webvenv/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Webvenv/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "/Webvenv/lib/python2.7/site-packages/django/db/models/base.py", line 124, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Webvenv/lib/python2.7/site-packages/django/db/models/base.py", line 330, in add_to_class
value.contribute_to_class(cls, name)
File "/Webvenv/lib/python2.7/site-packages/django/db/models/options.py", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Webvenv/lib/python2.7/site-packages/django/db/__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Webvenv/lib/python2.7/site-packages/django/db/utils.py", line 211, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Webvenv/lib/python2.7/site-packages/django/db/utils.py", line 115, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/include/python2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Webvenv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 30, in <module>
'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb.
Did you install mysqlclient or MySQL-python?
(Webvenv) [root@python2 WT]# yum install MySQLdb
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.aliyun.com
Setting up Install Process
No package MySQLdb available.
Error: Nothing to do
8.尝试安装;$ pip install MySQLdb && pip install MySQL-python
(Webvenv) [root@python2 WT]# pip install MySQL-pythonCollecting MySQL-python
Downloading MySQL-python-1.2.5.zip (108kB)
100% |████████████████████████████████| 112kB 369kB/s
Building wheels for collected packages: MySQL-python
Running setup.py bdist_wheel for MySQL-python ... done
Stored in directory: /root/.cache/pip/wheels/38/a3/89/ec87e092cfb38450fc91a62562055231deb0049a029054dc62
Successfully built MySQL-python
Installing collected packages: MySQL-python
Successfully installed MySQL-python-1.2.5
9.继续执行:不再报错了
(Webvenv) [root@python2 WT]# ./manage.py
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
虚拟环境很重要,尤其是在装了不同版本 python的机器上