ImportError: No module named 'MySQL'

后端 未结 17 1710
轮回少年
轮回少年 2020-12-12 17:40

I have downloaded the Connector/Python for MySQL successfully. I used the following code in Python\'s shell to test my connection:

import mysql.connector

相关标签:
17条回答
  • 2020-12-12 18:21

    I was facing the similar issue. My env details - Python 2.7.11 pip 9.0.1 CentOS release 5.11 (Final)

    Error on python interpreter -

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

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

    For python3 and later use the next command: $ pip3 install mysql-connector-python-rf

    0 讨论(0)
  • 2020-12-12 18:22

    The silly mistake I had done was keeping mysql.py in same dir. Try renaming mysql.py to another name so python don't consider that as module.

    0 讨论(0)
  • 2020-12-12 18:25

    I found that @gdxn96 solution worked for me, but with 1 change.

    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
    
    0 讨论(0)
  • 2020-12-12 18:25

    My project was using pipenv. The following command worked for me:

    pipenv install mysql-connector-python
    
    0 讨论(0)
  • 2020-12-12 18:29

    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
    
    0 讨论(0)
  • 2020-12-12 18:33
    sudo python3 -m pip install mysql-connector-python
    
    0 讨论(0)
提交回复
热议问题