Python 导入错误:没有名为 MySQLdb 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22252397/
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
ImportError: No module named MySQLdb
提问by user3182194
I am referring the following tutorial to make a login page for my web application. http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982
我指的是以下教程为我的 Web 应用程序制作登录页面。 http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982
I am having issue with the database. I am getting an
我的数据库有问题。我得到一个
ImportError: No module named MySQLdb
when I execute
当我执行
http://127.0.0.1:5000/testdb
I have tried all possible ways to install python mysql, the one mentioned in the tutorial, easy_install, sudo apt-get install.
我已经尝试了所有可能的安装python mysql的方法,教程中提到的一种,easy_install,sudo apt-get install。
I have installed mysql in my virtual env. My directory structure is just the same as whats explained in the tutorial. The module is sucessfully installed in my system and still I am getting this error.
我已经在我的虚拟环境中安装了 mysql。我的目录结构与教程中解释的相同。该模块已成功安装在我的系统中,但仍然出现此错误。
Please help. What could be causing this.
请帮忙。这可能是什么原因造成的。
采纳答案by DazWorrall
If you're having issues compiling the binary extension, or on a platform where you cant, you can try using the pure python PyMySQLbindings.
如果您在编译二进制扩展时遇到问题,或者在您无法编译的平台上,您可以尝试使用纯 pythonPyMySQL绑定。
Simply pip install pymysqland switch your SQLAlchemy URI to start like this:
简单地pip install pymysql切换你的 SQLAlchemy URI 以这样开始:
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://.....'
There are some other driversyou could also try.
回答by Edward van Kuik
Or try this:
或者试试这个:
apt-get install python-mysqldb
回答by Waseem Akram Malik
I got this issue when I was working on SQLAlchemy. The default dialect used by SQLAlchemy for MySQL is mysql+mysqldb.
我在使用 SQLAlchemy 时遇到了这个问题。SQLAlchemy for MySQL 使用的默认方言是mysql+mysqldb.
engine = create_engine('mysql+mysqldb://scott:tiger@localhost/foo')
I got the "No module named MySQLdb" error when the above command was executed. To fix it I installed the mysql-pythonmodule and the issue was fixed.
No module named MySQLdb执行上述命令时出现“ ”错误。为了修复它,我安装了mysql-python模块并且问题得到了解决。
sudo pip install mysql-python
回答by Adventurist
My issue is :
我的问题是:
return __import__('MySQLdb')
ImportError: No module named MySQLdb
and my resolution :
和我的决议:
pip install MySQL-python
yum install mysql-devel.x86_64
at the very beginning, i just installed MySQL-python, but the issue still existed. So i think if this issue happened, you should also take mysql-devel into consideration. Hope this helps.
一开始,我只是安装了 MySQL-python,但问题仍然存在。所以我认为如果发生这个问题,你也应该考虑 mysql-devel。希望这可以帮助。
回答by Doni
may you try
你可以试试吗
pip install mysqlclient
回答by seokhoonlee
It depends on Python Version as well in my experience.
根据我的经验,这也取决于 Python 版本。
If you are using Python 3, @DazWorrall answer worked fine for me.
如果您使用的是 Python 3,@DazWorrall 的回答对我来说效果很好。
However, if you are using Python 2, you should
但是,如果您使用的是 Python 2,则应该
sudo pip install mysql-python
which would install 'MySQLdb' module without having to change the SQLAlchemy URI.
这将安装“MySQLdb”模块而无需更改 SQLAlchemy URI。
回答by Flibertyjibbet
So I spent about 5 hours trying to figure out how to deal with this issue when trying to run
所以我花了大约 5 个小时试图弄清楚在尝试运行时如何处理这个问题
./manage.py makemigrations
With Ubuntu Server LTS 16.1, a full LAMP stack, Apache2 MySql 5.7 PHP 7 Python 3 and Django 1.10.2 I really struggled to find a good answer to this. In fact, I am still not satisfied, but the ONLY solution that worked for me is this...
使用 Ubuntu Server LTS 16.1、完整的 LAMP 堆栈、Apache2 MySql 5.7 PHP 7 Python 3 和 Django 1.10.2,我真的很难找到一个好的答案。事实上,我仍然不满意,但唯一对我有用的解决方案是...
sudo apt-get install build-essential python-dev libapache2-mod-wsgi-py3 libmysqlclient-dev
followed by (from inside the virtual environment)
其次是(从虚拟环境内部)
pip install mysqlclient
I really dislike having to use dev installs when I am trying to set up a new web server, but unfortunately this configuration was the only mostly comfortable path I could take.
当我尝试设置新的 Web 服务器时,我真的不喜欢必须使用开发安装,但不幸的是,这种配置是我可以采取的唯一最舒适的路径。
回答by Chen Levy
While @Edward van Kuik's answeris correct, it doesn't take into account an issue with virtualenv v1.7 and above.
虽然@Edward van Kuik的回答是正确的,但它没有考虑到virtualenv v1.7 及更高版本的问题。
In particular installing python-mysqldbvia apton Ubuntu put it under /usr/lib/pythonX.Y/dist-packages, but this path isn't included by default in the virtualenv's sys.path.
特别是安装python-mysqldb通过apt在Ubuntu上投入了它在/usr/lib/pythonX.Y/dist-packages,但默认情况下不会在virtualenv中的包括这条道路sys.path。
So to resolve this, you should create your virtualenv with system packages by running something like:
因此,要解决此问题,您应该通过运行以下内容来使用系统包创建 virtualenv:
virtualenv --system-site-packages .venv
virtualenv --system-site-packages .venv
回答by chandan kharbanda
Got so many errors related to permissions and what not. You may wanna try this :
有很多与权限相关的错误,什么不是。你可能想试试这个:
xcode-select --install
回答by hamed
yum install MySQL-python.x86_64
worked for me.
为我工作。

