导入错误:没有使用 Python2 的名为 mysql.connector 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24272223/
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-19 04:21:22  来源:igfitidea点击:

ImportError: No module named mysql.connector using Python2

pythonmysql

提问by Chase W.

I have two files. The first one has the connection and the getting of data. I import mysql.connector. This file is called tasksSql.py

我有两个文件。第一个有连接和数据的获取。我导入 mysql.connector。这个文件叫做tasksSql.py

def get_users():
    import mysql.connector

    con = mysql.connector.connect(user='****', password='*****',
                                  host='127.0.0.1',
                                  database='tasks')
    c = con.cursor()

    users = []    
    c.execute("""SELECT * FROM task_user""")

    for row in c:
        user = {
            'id': row[0],
            'first': row[1],
            'last': row[2],
            'email': row[3],
            'password': row[4],
            'creation_date': row[5]
        }
        users.append(user)
    c.close()
    return users

When I run this file singly it works and returns data.

当我单独运行这个文件时,它会工作并返回数据。

I have another file named tasks.py where I am going to be importing this file, however, this isn't working! When I import the file, it gives me the error:

我有另一个名为 tasks.py 的文件,我将在其中导入此文件,但是,这不起作用!当我导入文件时,它给了我错误:

ImportError: No module named mysql.connector

What am I doing wrong?

我究竟做错了什么?

采纳答案by Robin Carlier

Depending on your python version and how you installed it, it is possible that mysql connector isn't installed, you can install it with pip

根据您的 python 版本和安装方式,可能未安装 mysql 连接器,您可以使用pip安装它

To install mysql connector:

要安装 mysql 连接器:

pip install mysql-connector-python

回答by Ernesto Suarez

I use Python 3.4 for windows and installed mysql library from http://dev.mysql.com/downloads/connector/python/

我在 Windows 上使用 Python 3.4 并从http://dev.mysql.com/downloads/connector/python/安装了 mysql 库

回答by Hui Zheng

I used the following command to install python mysql-connector in Mac. it works

我使用以下命令在 Mac 中安装 python mysql-connector。有用

pip install mysql-connector-python-rf

pip 安装 mysql-connector-python-rf

回答by ritz301

This worked in ubuntu 16.04 for python 2.7:

这在 ubuntu 16.04 中适用于 python 2.7:

sudo pip install mysql-connector

回答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
>>>

回答by Ajo Paul

In my case, after the recent (Mac OS High Sierra) upgrade and the subsequent brew upgrade, I started to see the above error. I followed the above instructions but still got the same error message. Then I realised that I had to use python2 which points to the brew installed python rather than the os x installed one.

就我而言,在最近的 (Mac OS High Sierra) 升级和随后的 brew 升级之后,我开始看到上述错误。我按照上述说明操作,但仍然收到相同的错误消息。然后我意识到我必须使用 python2 它指向 brew 安装的 python 而不是 os x 安装的 python。

回答by xinthose

What worked for me was to download the .debfile directly and install it (dpkg -i mysql-connector-python_2.1.7-1ubuntu16.04_all.deb). Python downloads are located here(you will need to create a free MySQL login to download first). Make sure you choose the correct version, i.e. (python_2.1.7vs. python-py3_2.1.7). Only the "Architecture Independent" version worked for me.

对我有用的是.deb直接下载文件并安装它 ( dpkg -i mysql-connector-python_2.1.7-1ubuntu16.04_all.deb)。Python 下载位于此处(您需要先创建一个免费的 MySQL 登录名才能下载)。确保您选择了正确的版本,即 ( python_2.1.7vs. python-py3_2.1.7)。只有“架构独立”版本对我有用。

回答by chandu vutukuri

use the command below

使用下面的命令

python -m pip install mysql-connector 

回答by Kamlesh Ghate

I was facing the same issue on WAMP. Locate the connectors available with pip command. You can run this from any prompt if Python ENV variables are properly set.

我在 WAMP 上遇到了同样的问题。使用 pip 命令找到可用的连接器。如果 Python ENV 变量设置正确,您可以从任何提示运行它。

    pip search mysql-connector

    mysql-connector (2.2.9)                           - MySQL driver written in Python
    bottle-mysql-connector (0.0.4)                    - MySQL integration for Bottle.
    mysql-connector-python (8.0.15)                   - MySQL driver written in Python
    mysql-connector-repackaged (0.3.1)                - MySQL driver written in Python
    mysql-connector-async-dd (2.0.2)                  - mysql async connection

Run the following command to install mysql-connector

运行以下命令安装mysql-connector

    C:\Users\Admin>pip install mysql-connector

verify the installation

验证安装

    C:\Users\Admin>python
    Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit 
    (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import mysql.connector
    >>>

This solution worked for me.

这个解决方案对我有用。

回答by Atiksha

In my case i already installed the package

就我而言,我已经安装了该软件包

pip install mysql-connector
pip install mysql-connector-python

It giving me the same error so, i uninstall the both package by using

它给了我同样的错误,所以我使用

pip uninstall mysql-connector
pip uninstall mysql-connector-python

and then again install the second package only

然后再次只安装第二个包

pip install mysql-connector-python

This solution worked for me.

这个解决方案对我有用。