Python 导入错误:没有名为“MySQL”的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32877671/
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 'MySQL'
提问by Melanie
I have downloaded the Connector/Python for MySQL successfully. I used the following code in Python's shell to test my connection:
我已经成功下载了 MySQL 的连接器/Python。我在 Python 的 shell 中使用了以下代码来测试我的连接:
import mysql.connector
import mysql.connector
I received the following error message:
我收到以下错误消息:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
import mysql.connector
ImportError: No module named 'mysql'
I can't figure out why MySQL is not being recognized.
我不明白为什么 MySQL 不被识别。
回答by gdxn96
Try that out bud
试试那个芽
sudo wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
gunzip mysql-connector-python-2.1.3.tar.gz
tar xf mysql-connector-python-2.1.3.tar
cd mysql-connector-python-2.1.3
sudo python3 setup.py install
回答by Spac3
I found that @gdxn96 solution worked for me, but with 1 change.
我发现 @gdxn96 解决方案对我有用,但有 1 个变化。
sudo wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
tar -zxvf mysql-connector-python-2.1.3.tar
cd mysql-connector-python-2.1.3
sudo python3 setup.py install
回答by nihilis
just a note, I just installed mysql on an ubuntu 16.04 server. I tried different options in the following order:
请注意,我刚刚在 ubuntu 16.04 服务器上安装了 mysql。我按以下顺序尝试了不同的选项:
- using the package from repository
sudo apt-get install python-mysqldb: Was installed correctly, but unfortunatelly python returnedImportError: No module named 'mysql' - using the ubuntu package from Mysql:
wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python_2.1.4-1ubuntu16.04_all.deb. Installed correctly withsudo dpckg -i package_name, but python returned the same error. - using tar.gz file, installing with python, worked ok.
wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.6.tar.gz(used 2.1.4 at that time)tar -xf mysql-connector-python-2.1.6.tar.gzcd mysql-connector-python-2.1.6/sudo python3 setup.py install
- 使用存储库中的包
sudo apt-get install python-mysqldb:已正确安装,但不幸的是 python 返回ImportError: No module named 'mysql' - 使用从MySQL ubuntu的包:
wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python_2.1.4-1ubuntu16.04_all.deb。使用 正确安装sudo dpckg -i package_name,但 python 返回相同的错误。 - 使用 tar.gz 文件,使用 python 安装,工作正常。
wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.6.tar.gz(当时用的是2.1.4)tar -xf mysql-connector-python-2.1.6.tar.gzcd mysql-connector-python-2.1.6/sudo python3 setup.py install
Did not investigate why the first two failed, might try later. Hope this helps someone.
前两个失败的原因没查,以后可以试试。希望这可以帮助某人。
回答by Rishi
I was facing the similar issue. My env details - Python 2.7.11 pip 9.0.1 CentOS release 5.11 (Final)
我面临着类似的问题。我的环境细节 - Python 2.7.11 pip 9.0.1 CentOS release 5.11(最终版)
Error on python interpreter -
python解释器错误 -
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
>>>
Use pip to search the available module -
使用 pip 搜索可用模块 -
$ pip search mysql-connector | grep --color mysql-connector-python
mysql-connector-python-rf (2.2.2) - MySQL driver written in Python
mysql-connector-python (2.0.4) - MySQL driver written in Python
Install the mysql-connector-python-rf -
安装 mysql-connector-python-rf -
$ pip install mysql-connector-python-rf
Verify
核实
$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
Thanks =)
谢谢=)
回答by Алексей Присяжный
May be simple install from cli?
可以从 cli 简单安装吗?
pip install mysql-connector-python-rf
Package name differs from import library name
包名与导入库名不同
Or my universal variant in code:
或者我在代码中的通用变体:
import pip
pip.main(['install','mysql-connector-python-rf'])
For new version of pip:
对于新版本的 pip:
from pip._internal import main
main(['install','mysql-connector-python-rf'])
It's better - install needed modules in running python installation (if many)
更好 - 在运行 python 安装时安装所需的模块(如果有的话)
回答by Ashish Gupta
You need to use one of the following commands. Which one depends on different-2 OS and software you have and use.
您需要使用以下命令之一。哪一个取决于您拥有和使用的不同 2 操作系统和软件。
sudo easy_install mysql-python (mix os)
sudo pip install mysql-python (mix os)
sudo apt-get install python-mysqldb (Linux Ubuntu, ...)
cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
yum install MySQL-python (Linux Fedora, CentOS ...)
回答by Deepak Chauhan
The silly mistake I had done was keeping mysql.pyin same dir. Try renaming mysql.pyto another name so pythondon't consider that as module.
我犯的一个愚蠢的错误是保持mysql.py在同一个目录中。尝试重命名mysql.py为另一个名称,因此python不要将其视为模块。
回答by ilexcel
Use
pip3 install mysql-connectorto install the python packaged (if you are using Python 3. For Python 2 you can use pip).
使用
pip3 install mysql-connector安装打包了Python(如果你正在使用Python 3.对于Python 2,你可以使用pip)。
回答by Горыныч
I just moved source folder connectorfrom folder mysqlto site-packages.
And run import connector
我刚刚将源文件夹connector从文件夹移动mysql到site-packages. 并运行import connector
回答by James Newborn
Just simply check if MySQL connectors bundle is installed on your PC
只需检查您的 PC 上是否安装了 MySQL 连接器包

