Python django.core.exceptions.ImproperlyConfigured:加载 MySQLdb 模块时出错:没有名为 MySQLdb 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15312732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:45:38  来源:igfitidea点击:

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

pythonmysqldatabasedjango

提问by getitstarted

The problem Im facing while trying to connect to database for mysql. I have also given the database settings that i have used.

我在尝试连接到 mysql 数据库时遇到的问题。我还提供了我使用过的数据库设置。

 Traceback (most recent call last):
 File "manage.py", line 10, in <module>
 execute_from_command_line(sys.argv)
 File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/home/arundhati/Desktop/test/testprac/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/sql.py", line 9, in <module>
from django.db import models
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/utils.py", line 27, in load_backend
return import_module('.base', backend_name)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 17, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

Databse Settings::

数据库设置::

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'ar_test_db',                      # Or path to database file if using 
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or   '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

Thanks a lot for the help !!

非常感谢您的帮助 !!

采纳答案by Ron E

It looks like you don't have the python mysql package installed, try:

看起来您没有安装 python mysql 包,请尝试:

pip install mysql-python

or if not using a virtual environment (on *nix hosts):

或者如果不使用虚拟环境(在 *nix 主机上):

sudo pip install mysql-python

回答by Rashi

you have to install python-mysqldb - Python interface to MySQL

你必须安装 python-mysqldb - MySQL 的 Python 接口

Try
sudo apt-get install python-mysqldb

尝试
sudo apt-get install python-mysqldb

回答by Anoyz

Just to add to other answers, if you're using Django, it is advisable that you install mysql-python BEFORE installing Django.

只是为了添加其他答案,如果您使用的是 Django,建议您在安装 Django 之前安装 mysql-python。

回答by Khamidulla

When I set up Django development environment for PyCharm in Mac OS X Mountain Lion with python, mysql, sequel pro application I got error same as owner of this thread. However, my answer for them who is running python-mysqldb under Mac OS Mountain Lion x86_x64 (MySql and Python also should be same architecture) and already tried everything like pip and etc. In order fix this problem do following steps:

当我使用 python、mysql、sequel pro 应用程序在 Mac OS X Mountain Lion 中为 PyCharm 设置 Django 开发环境时,我得到与该线程所有者相同的错误。但是,我对在 Mac OS Mountain Lion x86_x64(MySql 和 Python 也应该是相同的架构)下运行 python-mysqldb 并且已经尝试过像 pip 等所有东西的人的回答。为了解决这个问题,请执行以下步骤:

  1. Download MySql for Python from here
  2. Untar downloaded file. In terminal window do following: tar xvfz downloade.tar.
  3. cd /to untared directory
  4. Run sudo python setup.py install
  5. If you get error something like this: "Environment Error: /usr/local/bin/mysql_config not found" then try to add path ass follows: "export PATH=$PATH:/usr/local/mysql/bin". But id did not helped to me and I found another solution. In the end of command execution error output which looks like this:

    File "/path_to_file/MySQL-python-1.2.4b4/setup_posix.py", line 25, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,))

  6. Open setup_posix.py with vim and go to line 25 (In your case it can be different unless if it is same version).

  7. Line 25 should look like this after your editing unless your mysql have symbolic link like follows '/usr/local/mysql/bin/':

    f = popen("%s --%s" % ('/usr/local/mysql/bin/mysql_config', what))

  8. After this I got another error as following:

    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so Reason: image not found

  9. Finally I did following in console:

    sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

  1. 这里下载 MySql for Python
  2. 解压下载的文件。在终端窗口中执行以下操作:tar xvfz downloade.tar。
  3. cd /to 解压目录
  4. 运行 sudo python setup.py install
  5. 如果你得到这样的错误:“环境错误:/usr/local/bin/mysql_config not found”然后尝试添加如下路径:“export PATH=$PATH:/usr/local/mysql/bin”。但是 id 对我没有帮助,我找到了另一个解决方案。在命令执行结束时,错误输出如下所示:

    文件“/path_to_file/MySQL-python-1.2.4b4/setup_posix.py”,第 25 行,在 mysql_config 中引发 EnvironmentError("%s not found" % (mysql_config.path,))

  6. 使用 vim 打开 setup_posix.py 并转到第 25 行(在您的情况下,除非版本相同,否则它可以不同)。

  7. 编辑后第 25 行应如下所示,除非您的 mysql 具有如下所示的符号链接“/usr/local/mysql/bin/”:

    f = popen("%s --%s" % ('/usr/local/mysql/bin/mysql_config', what))

  8. 在此之后,我遇到了另一个错误,如下所示:

    django.core.exceptions.ImproperlyConfigured:加载 MySQLdb 模块时出错:dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so, 2) :库未加载:libmysqlclient.18.dylib 引用自:/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so 原因:找不到图像

  9. 最后我在控制台中做了以下操作:

    须藤ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Currently everything works fine. So I hope it will be helpful for somebody who uses Mac. :)

目前一切正常。所以我希望它对使用Mac的人有所帮助。:)

回答by ruddra

Download and install Mysql-python from here for windows environment. http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python.

从这里下载并安装用于 Windows 环境的 Mysql-python。 http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

回答by Gaurav Agarwal

On Ubuntu it is advised to use the distributions repository.

在 Ubuntu 上,建议使用发行版存储库。

sudo apt-get install python-mysqldb

回答by Haimei

You are missing the python mysqldblibrary. Use this command (for Debian/Ubuntu) to install it: sudo apt-get install python-mysqldb

您缺少 pythonmysqldb库。使用此命令(适用于 Debian/Ubuntu)来安装它: sudo apt-get install python-mysqldb

回答by Will

My answer is similar to @Ron-E, but I got a few more errors/corrections so I'm putting my steps below for Mac OSX on Mavericks and Python 2.7.6.

我的答案与@Ron-E 类似,但我得到了更多错误/更正,因此我将在 Mavericks 和 Python 2.7.6 上针对 Mac OSX 执行以下步骤。

  1. Install Python mysql package (if you get a success message, then ignore the below steps)

    pip install mysql-python
    
  2. When I did the above, I got the error "EnvironmentError: mysql_config not found" enter image description hereTo fix this, I did the below in terminal:

    export PATH=$PATH:/usr/local/mysql/bin
    
  3. When I reran step 1, I get a new error "error: command 'cc' failed with exit status 1" enter image description hereTo fix this, I did the below in terminal:

     export CFLAGS=-Qunused-arguments
     export CPPFLAGS=-Qunused-arguments
    
  4. I reran step 1 and got the success message 'Successfully installed mysql-python'!

  1. 安装 Python mysql 包(如果收到成功信息,则忽略以下步骤)

    pip install mysql-python
    
  2. 当我执行上述操作时,出现错误“EnvironmentError: mysql_config not found” 在此处输入图片说明为了解决这个问题,我在终端中执行了以下操作:

    export PATH=$PATH:/usr/local/mysql/bin
    
  3. 当我重新运行第 1 步时,我收到一个新错误“错误:命令 'cc' 失败,退出状态为 1” 在此处输入图片说明为了解决这个问题,我在终端中执行了以下操作:

     export CFLAGS=-Qunused-arguments
     export CPPFLAGS=-Qunused-arguments
    
  4. 我重新运行第 1 步并收到成功消息“成功安装 mysql-python”!

回答by cmdAmir

With the same error message as Will, it worked for me to install mysql first as the missing file will be added during the installation. So after

使用与 Will 相同的错误消息,我首先安装 mysql 是有用的,因为在安装过程中将添加丢失的文件。所以之后

brew install mysql

pip install mysql-python

ran without errors.

运行没有错误。

回答by Psddp

If you get errors trying to install mysqlclient with pip, you may lack the mysql dev library. Install it by running:

如果您在尝试使用 pip 安装 mysqlclient 时遇到错误,则您可能缺少 mysql dev 库。通过运行安装它:

apt-get install libmysqlclient-dev

and try again to install mysqlclient:

并再次尝试安装 mysqlclient:

pip install mysqlclient