加载 MySQLdb 模块时出错“您是否安装了 mysqlclient 或 MySQL-python?”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46902357/
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
Error loading MySQLdb Module 'Did you install mysqlclient or MySQL-python?'
提问by PrimeBenefiter
I am using windows 10 command line for a django project using python34 however, I am facing difficulties with SQL.
我在使用 python34 的 django 项目中使用 windows 10 命令行,但是,我在使用 SQL 时遇到了困难。
I have already installed mysqlclient using pip install mysqlclient==1.3.5
and located the file to make sure I was not delusional. I then ran python manage.py migrate
to migrate the tables to the SQL database (I am using phpmyadmin). However when the command returned with...
我已经使用pip install mysqlclient==1.3.5
并找到了该文件来安装 mysqlclient ,以确保我没有错觉。然后我跑去python manage.py migrate
将表迁移到 SQL 数据库(我使用的是 phpmyadmin)。但是,当命令返回时...
File "C:\Users\user\env\lib\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?
I am aware that questions like these already exist, but not one solution seems to have made any impact on attempts. Thank you in advance.
我知道这样的问题已经存在,但似乎没有一种解决方案对尝试产生任何影响。先感谢您。
回答by aforwardz
pip install pymysql
Then, edit the __init__.py file in your project origin dir(the same as settings.py)
然后,编辑项目源目录中的 __init__.py 文件(与 settings.py 相同)
add:
添加:
import pymysql
pymysql.install_as_MySQLdb()
回答by Aniket Sinha
Faced same problem after migrating to python 3. Apparently, MySQL-python is incompatible, so as per official django docs, installed mysqlclientusing pip install mysqlclient
on Mac. Note that there are some OS specific issues mentioned in docs.
迁移到 python 3 后遇到同样的问题。显然,MySQL-python 不兼容,因此根据官方django 文档,在 Mac 上安装了mysqlclient使用pip install mysqlclient
。请注意,文档中提到了一些特定于操作系统的问题。
Quoting from docs:
引用文档:
Prerequisites
You may need to install the Python and MySQL development headers and libraries like so:
sudo apt-get install python-dev default-libmysqlclient-dev
# Debian / Ubuntu
sudo yum install python-devel mysql-devel
# Red Hat / CentOS
brew install mysql-connector-c
# macOS (Homebrew) (Currently, it has bug. See below)On Windows, there are binary wheels you can install without MySQLConnector/C or MSVC.
Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command :
sudo apt-get install python3-dev
# debian / Ubuntu
sudo yum install python3-devel
# Red Hat / CentOSNote about bug of MySQL Connector/C on macOS
See also: https://bugs.mysql.com/bug.php?id=86971
Versions of MySQL Connector/C may have incorrect default configuration options that cause compilation errors when
mysqlclient-python
is installed. (As of November 2017, this is known to be true for homebrew'smysql-connector-c
and official package)Modification of
mysql_config
resolves these issues as follows.Change
先决条件
您可能需要像这样安装 Python 和 MySQL 开发头文件和库:
sudo apt-get install python-dev default-libmysqlclient-dev
# Debian / Ubuntu
sudo yum install python-devel mysql-devel
# 红帽 / CentOS
brew install mysql-connector-c
# macOS (Homebrew)(目前有bug。见下文)在 Windows 上,您可以在没有 MySQLConnector/C 或 MSVC 的情况下安装二进制轮。
关于 Python 3 的注意事项:如果您使用的是 python3,则需要使用以下命令安装 python3-dev:
sudo apt-get install python3-dev
# debian / Ubuntu
sudo yum install python3-devel
# 红帽 / CentOS关于 macOS 上 MySQL Connector/C 的 bug 的注意事项
另见:https: //bugs.mysql.com/bug.php?id=86971
MySQL Connector/C 的版本可能有不正确的默认配置选项,在
mysqlclient-python
安装时会导致编译错误。(截至 2017 年 11 月,众所周知,自制软件mysql-connector-c
和官方软件包都是如此)修改
mysql_config
解决这些问题如下。改变
# on macOS, on or about line 112:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "
to
到
# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
An improper ssl configuration may also create issues; see, e.g,
brew info openssl
for details on macOS.Install from PyPI
pip install mysqlclient
NOTE: Wheels for Windows may be not released with source package. You should pin version in your
requirements.txt
to avoid trying to install newest source package.Install from source
- Download source by
git clone
or zipfile.- Customize
site.cfg
python setup.py install
不正确的 ssl 配置也可能会产生问题;参见,例如,
brew info openssl
有关 macOS 的详细信息。从 PyPI 安装
pip install mysqlclient
注意:Windows 的 Wheels 可能不随源包一起发布。您应该在您的版本中固定版本
requirements.txt
以避免尝试安装最新的源包。从源安装
- 通过
git clone
或zipfile下载源代码。- 定制
site.cfg
python setup.py install
回答by user2317231
Use the below command to solve your issue,
使用以下命令来解决您的问题,
pip install mysql-python
apt-get install python3-mysqldb libmysqlclient-dev python-dev
Works on Debian
适用于 Debian
回答by Md Humayun Kabir
For pip
对于点
pip install pymysql
For pip3 you should use
对于 pip3,您应该使用
python3 -m pip install PyMySQL
Then, edit the init.py file in your project origin directory (the same as settings.py). Add:
然后,编辑项目原始目录中的 init.py 文件(与 settings.py 相同)。添加:
import pymysql
pymysql.install_as_MySQLdb()
回答by Bandham Manikanta
Installing mysqlclient
using anaconda
is pretty simple and straight forward
.
安装mysqlclient
usinganaconda
很漂亮simple and straight forward
。
conda install -c bioconda mysqlclient
and then, install pymysql
using pip.
然后,pymysql
使用pip安装。
pip install pymysql
happy learning..
快乐学习。。
回答by Dat TT
For MAC os user:
对于 MAC os 用户:
I have faced this issue many times. The key here is to set these environment variables before installing mysqlclient by pip command. For my case, they are like below:
我多次遇到过这个问题。这里的关键是在通过pip命令安装mysqlclient之前设置这些环境变量。就我而言,它们如下所示:
export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
export CPPFLAGS="-I/usr/local/opt/[email protected]/include"
导出 LDFLAGS="-L/usr/local/opt/[email protected]/lib"
导出 CPPFLAGS="-I/usr/local/opt/[email protected]/include"
回答by CodeBiker
I simply needed to update my project's dependencies and then restart the server.
我只需要更新我的项目的依赖项,然后重新启动服务器。
回答by Xoadra
I had this issue just recently even with using the python 3 compatible mysqlclient
library and managed to solve my issue albeit in a bit of an unorthodox manner. If you are using MySQL 8, give this a try and see if it helps! :)
即使使用 python 3 兼容mysqlclient
库,我最近也遇到了这个问题,并设法解决了我的问题,尽管以一种非正统的方式解决了这个问题。如果您使用的是 MySQL 8,请尝试一下,看看它是否有帮助!:)
I simply made a copy of the libmysqlclient.21.dylib
file located in my up-to-date installation of MySQL 8.0.13 which is was in /usr/local/mysql/lib
and moved that copy under the same name to /usr/lib
.
我只是复制了libmysqlclient.21.dylib
位于我最新安装的 MySQL 8.0.13中的文件的副本,/usr/local/mysql/lib
并将该副本以相同的名称移动到/usr/lib
.
You will need to temporarily disable security integrity protection on your mac however to do this since you won't have or be able to change permissions to anything in /usr/lib
without disabling it. You can do this by booting up into the recovery system, click Utilities on the menu at the top, and open up the terminal and enter csrutil disable
into the terminal. Just remember to turn security integrity protection back on when you're done doing this! The only difference from the above process will be that you run csrutil enable
instead.
您需要暂时禁用 mac 上的安全完整性保护,但是要这样做,因为/usr/lib
如果不禁用它,您将无法或无法更改对任何内容的权限。您可以通过启动进入恢复系统来执行此操作,单击顶部菜单上的实用程序,然后打开终端并进入csrutil disable
终端。请记住在完成此操作后重新打开安全完整性保护!与上述过程的唯一区别是您运行csrutil enable
。
You can find out more about how to disable and enable macOS's security integrity protection here.
回答by nguy?n ng?c tú
edit the init.py file in your project origin directory
编辑项目原始目录中的 init.py 文件
import pymysql
pymysql.install_as_MySQLdb()