您的位置:首页 > 编程语言 > Python开发

Python,Django安装MySQLdb各种经典错误(Error No module named MySQLdb)

2014-05-20 19:57 891 查看
Python,Django安装MySQLdb各种经典错误(Error No module named MySQLdb)

前文:

这几日有些闲,于就想把前面断断续续学习Django WEB框架真正搭建起来,并使用mysql数据库。可是,真正在搭建过程中遇到了各式各样的错误。因此,在此特别记录一下。

(python shell 里能正常import MySQLdb 模块,但是Django里manage.py runserver 报错:No module named MySQLdb,见第四错)

准备工作:

首先肯定得先安一个python,实际上我机子上已经有了2.7版的了,不过为学习Django我又上官网去下载了一个python2.6版本的。

之后又去Django官网下载了最新的Django框架,并上http://sourceforge.net/projects/mysql-python/
去下载了window 版本的MySQLdb。

Django,MySQLdb都到 python2.6安装目录下的 lib/site-packages/里(实际上:下载Django window版后,解压里面的文件目录只copy

django目录就OK了)。

一切安装好后,开始import 工作了。

第一个错误:

Traceback (most recent call last):

File "<pyshell#1>", line 1, in <module>

import MYSQLdb

ImportError: No module named MYSQLdb

原因

模块名称写错了把MySQLdb写成了MYSQLdb

Hmm, import's failing -- meaning you don't have the psycopg module installed. Not sure what your django app has or how you installed the postgresql_psycopg egg.

FWIW, there's an openshift sample app on github and here's a recipe to get this working w/ postgres:

1. Create a python application: rhc app create rango python-2.6

2. Embed postgresql via: rhc cartridge add postgresql-8.4 -a rango

3. Add a remote to the github openshift-django-example and pull from it:

sh> cd rango/

sh> git remote add upstream -m master https://github.com/openshift/django -example.git

sh> git pull -s recursive -X theirs upstream master

4. Edit the setup.py file to add psycopg2. Relevant line should read:

install_requires=['Django>=1.3', 'psycopg2'],

5. Edit the rango/wsgi/openshift/settings.py file to specify the db type + parameters. Its a good idea to use variables here instead of hardcoded values.

'ENGINE': 'django.db.backends.postgresql_psycopg2',

'NAME': os.environ['OPENSHIFT_APP_NAME'],

'USER': os.environ['OPENSHIFT_DB_USERNAME'],

'PASSWORD': os.environ['OPENSHIFT_DB_PASSWORD'],

'HOST': os.environ['OPENSHIFT_DB_HOST'],

'PORT': os.environ['OPENSHIFT_DB_PORT'],

6. Edit the rango/.openshift/action_hooks/deploy script to setup the db. Add

these lines to that script:

source $OPENSHIFT_APP_DIR/virtenv/bin/activate

export PYTHON_EGG_CACHE=$OPENSHIFT_APP_DIR/virtenv/lib/python-2.6

echo "Executing 'manage.py syncdb --noinput'"

python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py syncdb --noinput

echo "Executing 'manage.py collectstatic --noinput'"

python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py collectstatic --noinput

7. Comment all the lines in the rango/.openshift/action_hooks/build script.

These are the lines we moved into the deploy step/hook.

8. Commit and deploy your code: cd rango && git commit . -m 'django to rango' && git push

9. One last step is you need to ssh in and create a superuser to login via the

admin -- suspect you can script this as well in the deploy.sh script.

ssh $guid@$appdns # this is the username/host in the git url for your app.

source $OPENSHIFT_APP_DIR/virtenv/bin/activate

cd ~/rango/repo/wsgi/openshift

python manage.py createsuperuser --username=bossmaan --noinput --email
me@example.org

python manage.py changepassword bossmaan

And that should get you up and running w/ django -- will try and get these steps simplified on the github sample.

Hope that helps.

解决方法:大虾们一定学好E文!


第二错误:

import MySQLdb

Traceback (most recent call last):

File "<pyshell#2>", line 1, in <module>

import MySQLdb

File "D:\PythonDev26\Lib\site-packages\MySQLdb\__init__.py", line 19, in <module>

import _mysql

ImportError: DLL load failed: 找不到指定的模块。

原因:花了一定时间去google,baidu,发现原来window 版本的MySQLdb需要两个dll文件的支持。

解决方法:参考《在python中使用MySQL》,再运行 import MySQLdb,又出现错误信息了。


第三错误:

Warning (from warnings module):

File "D:\PythonDev26\Lib\site-packages\MySQLdb\__init__.py", line 34

from sets import ImmutableSet

DeprecationWarning: the sets module is deprecated

原因:看错误提示貌似是说 sets module 被弃用了。

解决方法:又花了一定时间去google,baidu。最终参考《在python中使用MySQL》

现在再在python shell里再运行“import MySQLdb” ,OK~~一切正常~

好了,现在要在Django里使用mysql了,不过在开始之前我们还得看看安装是否正常:

import sys

import django

import MySQLdb

print sys.version_info

print django.VERSION

print MySQLdb.version_info

输出:

(2, 6, 6, 'final', 0)

(1, 2, 7, 'final', 0)

(1, 2, 2, 'final', 0)

恩,只少说到目前为止一切正常。


先按照官网手册进行,进入django/bin 目录里,打开cmd。输入 python django-admin.py startproject mysite 开始一个项目,进入刚才新建项目 mysite目录里,在cmd里运行 python manage.py runserver
开始一个开发用的web server。

恩,没有什么问题,通过http://localhost:8000/也能访问到像其官网所说“淡蓝色的,柔和”的页面。OK,我们开始配置数据库吧,进入settings.py里找到12行进行mysql数据库的配置:

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql', # 数据库API'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.

'NAME': 'test', # 数据库

'USER': 'user', # 用户名

'PASSWORD': '', # 密码

'HOST': 'localhost', # 主机地址

'PORT': '3306', # 端口

}

}

好,我们再重新run一次:python manage.py runserver

OH。。。。MY GOD......

怎么了????

第四个错误:

.... backend = __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE),

{}, {}, [''])

File "D:\Python\Python24\Lib\site-packages\django\db\backends\mysql\base.py",

line 13, in ?

raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No mo

dule named MySQLdb


原因:这是什么??怎么会这样?python shell 里import MySQLdb 也没有任何错误呀?????


查网络了半天,http://stackoverflow.com/questions/770904/installing-django-on-shared-server-no-module-named-mysqldb这家伙好像有这问题。最后在“laofo”
里找出原因:原来我之前装过python2.7,又在python里装过Django,并且设置过window环境变量路径Path和PYTHONPATH为python2.7的,但是没有装过MySQLdb,所以文件被关联到python2.7里了。

解决方法:重新设置一下环境变量,确保安装路径等是正确的。

PS:No module named MySQLdb 最经典
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐