加载MySQLdb模块错误:没有名为MySQLdb的模块

时间:2022-09-22 12:26:12

I'm using Django 1.4.1 with Active Python 2.7 on Win7. I have installed the MySQL module using pypm install mysql-python.

我在Win7上使用Django 1.4.1和Active Python 2.7。我使用pypm安装了sql-python安装了MySQL模块。

The database engine is django.db.backends.mysql.

数据库引擎是django.db.backends.mysql。

import MySQLdb works in the interactive shell.

导入MySQLdb在交互shell中工作。

.\manage.py syncdb created the tables with no problem.

\管理。py syncdb创建的表没有问题。

However, when I open the site in the browser, I get Error loading MySQLdb module: No module named MySQLdb:

但是,当我在浏览器中打开站点时,加载MySQLdb模块时出现错误:没有名为MySQLdb的模块:

Environment:


Request Method: GET
Request URL: http://whatever/

Django Version: 1.4.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  89.                     response = middleware_method(request)
File "C:\Python27\lib\site-packages\django\contrib\sessions\middleware.py" in process_request
  10.         engine = import_module(settings.SESSION_ENGINE)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)
File "C:\Python27\lib\site-packages\django\contrib\sessions\backends\cached_db.py" in <module>
  6. from django.contrib.sessions.backends.db import SessionStore as DBStore
File "C:\Python27\lib\site-packages\django\contrib\sessions\backends\db.py" in <module>
  3. from django.db import IntegrityError, transaction, router
File "C:\Python27\lib\site-packages\django\db\__init__.py" in <module>
  40. backend = load_backend(connection.settings_dict['ENGINE'])
File "C:\Python27\lib\site-packages\django\db\__init__.py" in __getattr__
  34.         return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Python27\lib\site-packages\django\db\utils.py" in __getitem__
  92.         backend = load_backend(db['ENGINE'])
File "C:\Python27\lib\site-packages\django\db\utils.py" in load_backend
  24.         return import_module('.base', backend_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py" in <module>
  16.     raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)

Exception Type: ImproperlyConfigured at /
Exception Value: Error loading MySQLdb module: No module named MySQLdb

The settings for the sessions and messages apps are:

会话和消息应用程序的设置是:

SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
MESSAGE_STORAGE = "django.contrib.messages.storage.cookie.CookieStorage"

How is this possible?

这怎么可能?

5 个解决方案

#1


3  

The problem was that MySQLdb was installed in my home directory C:\Users\alexei\AppData\Roaming\Python\Python27\site-packages\ which was not in Python's path. So I uninstalled it with pypm uninstall mysql-python and then reinstalled it globally using pypm -g install mysql-python (note the -g option).

问题是MySQLdb是安装在我的主目录C:\Users\alexei\AppData\Roaming\Python\Python27\网站\并没有在Python的路径。因此,我使用pypm卸载sql-python卸载它,然后使用pypm -g安装mysql-python(注意-g选项)在全局重新安装它。

The alternative is to add that path to the list sys.path.append("...path...") in wsgi.py

另一种方法是将该路径添加到list sys.path.append(“…path…path…”)中的wsgi.py中

So, in case someone else is wondering, you can find out where MySQLdb (or any other module) is installed like so:

因此,如果有人想知道,您可以找到MySQLdb(或任何其他模块)的安装位置,如下所示:

import MySQLdb
print MySQLdb.__file__

Make sure that that path is in Python's path list provided in Django's error message.

确保该路径在Django的错误消息中提供的Python的路径列表中。

#2


2  

If you are using and virtual environment. you must run

如果您正在使用和虚拟环境。你必须运行

cd virtualEnvPath
pip install MySQL-Python

Don't forget positioning inside of your virtual env. /bin PATH, to use local pip. REGARDS!

不要忘记在虚拟环境中定位。/bin路径,使用本地pip。的问候!

#3


1  

i also faced this issue then i installed python mysql to my system

我也遇到了这个问题,然后我将python mysql安装到我的系统中

pip install mysql-python

pip安装mysql-python

then its workes see this --> working in my system >>
$ python manage.py syncdb;

Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
.............working

然后,它的工作看到了这个——>在我的系统>> $ python管理。py syncdb;创建表……创建表django_admin_log创建表auth_permission创建表

#4


1  

I configured djando w/ mysql the following way:

我配置djando w/ mysql如下:

1) set DJANGO_SETTINGS_MODULE environment variable in .bashrc and source it

1)在.bashrc中设置DJANGO_SETTINGS_MODULE环境变量,并对其进行源代码处理

2) change settings.py pointed by this env_var and local_settings.py (if any) like this (for mysql):

2)改变设置。py指向这个env_var和local_settings。py(如有):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

3) install mysql dev libraries and header files, ensure that mysql is on PATH

3)安装mysql开发库和头文件,确保mysql在路径上

sudo apt-get install libmysqlclient-dev

4) install python package for mysql

4)为mysql安装python包

sudo pip install MySQL-python

5) test django's access to your mysql db:

5)测试django对mysql db的访问:

python manage.py dbshell

or

django-admin dbshell

6) sync django to mysql from dir with your django app

6)用django应用程序将django同步到mysql。

python manage.py syncdb

#5


0  

Easiest way is to install MySQLdb from binary: http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

最简单的方法是从二进制文件http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python中安装MySQLdb

#1


3  

The problem was that MySQLdb was installed in my home directory C:\Users\alexei\AppData\Roaming\Python\Python27\site-packages\ which was not in Python's path. So I uninstalled it with pypm uninstall mysql-python and then reinstalled it globally using pypm -g install mysql-python (note the -g option).

问题是MySQLdb是安装在我的主目录C:\Users\alexei\AppData\Roaming\Python\Python27\网站\并没有在Python的路径。因此,我使用pypm卸载sql-python卸载它,然后使用pypm -g安装mysql-python(注意-g选项)在全局重新安装它。

The alternative is to add that path to the list sys.path.append("...path...") in wsgi.py

另一种方法是将该路径添加到list sys.path.append(“…path…path…”)中的wsgi.py中

So, in case someone else is wondering, you can find out where MySQLdb (or any other module) is installed like so:

因此,如果有人想知道,您可以找到MySQLdb(或任何其他模块)的安装位置,如下所示:

import MySQLdb
print MySQLdb.__file__

Make sure that that path is in Python's path list provided in Django's error message.

确保该路径在Django的错误消息中提供的Python的路径列表中。

#2


2  

If you are using and virtual environment. you must run

如果您正在使用和虚拟环境。你必须运行

cd virtualEnvPath
pip install MySQL-Python

Don't forget positioning inside of your virtual env. /bin PATH, to use local pip. REGARDS!

不要忘记在虚拟环境中定位。/bin路径,使用本地pip。的问候!

#3


1  

i also faced this issue then i installed python mysql to my system

我也遇到了这个问题,然后我将python mysql安装到我的系统中

pip install mysql-python

pip安装mysql-python

then its workes see this --> working in my system >>
$ python manage.py syncdb;

Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
.............working

然后,它的工作看到了这个——>在我的系统>> $ python管理。py syncdb;创建表……创建表django_admin_log创建表auth_permission创建表

#4


1  

I configured djando w/ mysql the following way:

我配置djando w/ mysql如下:

1) set DJANGO_SETTINGS_MODULE environment variable in .bashrc and source it

1)在.bashrc中设置DJANGO_SETTINGS_MODULE环境变量,并对其进行源代码处理

2) change settings.py pointed by this env_var and local_settings.py (if any) like this (for mysql):

2)改变设置。py指向这个env_var和local_settings。py(如有):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

3) install mysql dev libraries and header files, ensure that mysql is on PATH

3)安装mysql开发库和头文件,确保mysql在路径上

sudo apt-get install libmysqlclient-dev

4) install python package for mysql

4)为mysql安装python包

sudo pip install MySQL-python

5) test django's access to your mysql db:

5)测试django对mysql db的访问:

python manage.py dbshell

or

django-admin dbshell

6) sync django to mysql from dir with your django app

6)用django应用程序将django同步到mysql。

python manage.py syncdb

#5


0  

Easiest way is to install MySQLdb from binary: http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

最简单的方法是从二进制文件http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python中安装MySQLdb