Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ImportError:没有使用Python2的名为mysql.connector的模块_Python_Mysql - Fatal编程技术网

ImportError:没有使用Python2的名为mysql.connector的模块

ImportError:没有使用Python2的名为mysql.connector的模块,python,mysql,Python,Mysql,我有两个文件。第一个是连接和获取数据。我导入mysql.connector。此文件名为tasksSql.py def get_users(): import mysql.connector con = mysql.connector.connect(user='****', password='*****', host='127.0.0.1',

我有两个文件。第一个是连接和获取数据。我导入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
当我单独运行这个文件时,它工作并返回数据

我有另一个名为tasks.py的文件,我将在其中导入此文件,但是,这不起作用!导入文件时,会出现以下错误:

ImportError: No module named mysql.connector

我做错了什么?

根据您的python版本和安装方式,可能没有安装mysql connector,您可以使用

要安装mysql连接器,请执行以下操作:

pip install mysql-connector-python

我使用Python3.4 for windows并从安装mysql库

我使用以下命令在Mac中安装Python mysql connector。它起作用了

pip安装mysql连接器python rf


这在适用于python 2.7的ubuntu 16.04中起作用:

sudo pip install mysql-connector

我也面临着类似的问题。我的环境详情- Python 2.7.11 pip 9.0.1 CentOS 5.11版(最终版)

python解释器出错-

>>> import mysql.connector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
>>>
安装mysql连接器-

$ pip install mysql-connector-python-rf
核实

$ 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
>>>

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

对我有效的方法是直接下载
.deb
文件并安装它(
dpkg-i mysql-connector-python_2.1.7-1ubuntu16.04_all.deb
)。Python下载已经找到(您需要先创建一个免费的MySQL登录才能下载)。确保选择了正确的版本,即(
python_2.1.7
vs.
python-py3_2.1.7
)。只有“独立于体系结构”的版本适合我。

使用下面的命令

python -m pip install mysql-connector 

我在WAMP上也面临同样的问题。使用pip命令找到可用的连接器。如果正确设置了Python环境变量,则可以在任何提示符下运行此命令

    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
运行以下命令安装mysql连接器

    C:\Users\Admin>pip install mysql-connector
验证安装

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

这个解决方案对我有效。

在我的情况下,我已经安装了这个软件包

pip install mysql-connector
pip install mysql-connector-python
它给我同样的错误,所以,我卸载这两个包使用

pip uninstall mysql-connector
pip uninstall mysql-connector-python
然后再次只安装第二个软件包

pip install mysql-connector-python

这个解决方案对我有效。

如果您在PYCHARM:ImportError上有此错误:没有名为mysql.connector的模块

尝试以下解决方案: 打开Pycharm进入文件->设置->项目->Pycharm内的Python解释器,然后按+图标安装mysql连接器。
问题解决了

我的MySQL安装有问题,我遇到了以下错误:

<module> import mysql.connector ModuleNotFoundError: No module named 'mysql' (zrealestate) 
[root@localhost zrealestate]# brew install mysql
-bash: brew: command not found

文件夹中有一个名为mysql.py的文件。这就是为什么它会出现错误,因为它试图在导入过程中调用它

import mysql.connector

我通过更改文件名解决了这个问题。

我有一个类似的问题,解决方案是将mysql connector python安装到实际的脚本环境中。 检查脚本的第一行是否有应该使用的解释器。大概是

#!/usr/bin/python3
import mysql.connector
在本例中,将mysql连接器python安装到python解释器的环境中。您应该使用pip或系统包管理器

pip3 install mysql-connector-python
或者类似的事情

dnf install mysql-connector-python3.noarch

然后通过运行一个propper python解释器来测试它。

我确实用pip安装了它,但它说它已经安装好了。您使用的是python 3.x吗?如果您是,您是否使用pip3.x命令安装了它?这样做了好几次,并且一直说没有安装模块,不确定我做错了什么!它让我一次又一次地安装它,并没有说它已经安装好了,所以我是不是遗漏了什么?@Josh Fisher先生,我也有同样的问题,你找到解决方案了吗?@MrJoshFisher我相信对于python3来说,安装包只是
mysql connector
我认为你应该从指向OP开始回答,他们可能没有安装包;在那之后,提出你的解决方案,可能需要根据具体情况进行一些调整。它在ubuntu 16.04 for Python2.7上也适用于我。你使用Python2解决了这个问题吗?请确认问题是否已解决或仍然存在。下载不需要您登录。请检查以下位置的直接下载链接:bottom@harpratap我从来没有注意到。谢谢。mysql-connector-python现在更新到8.0.11,rf变体似乎不再更新了。为什么是
-rf
版本而不是普通版本?有什么区别?谢谢。因为你的回答,修正了我的问题。哦,我的上帝,我也有同样的问题,很好的工作,这里也一样,非常感谢你!哇,我也有同样的问题。如果没有这篇帖子的回复,我永远也想不到这一点!
dnf install mysql-connector-python3.noarch