ImportError: No module named 'MySQL'

后端 未结 17 1711
轮回少年
轮回少年 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:34

    You need to use one of the following commands. Which one depends on different-2 OS and software you have and use.

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

    I just moved source folder connector from folder mysql to site-packages. And run import connector

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

    run

    pip list 
    

    to see list of packages you have installed. If it has mysql-connector-python then that is fine.

    Remember not to name your python script file as mysql.py

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

    Use pip3 install mysql-connector to install the python packaged (if you are using Python 3. For Python 2 you can use pip).

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

    just a note, I just installed mysql on an ubuntu 16.04 server. I tried different options in the following order:

    • using the package from repository sudo apt-get install python-mysqldb: Was installed correctly, but unfortunatelly python returned ImportError: 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 with sudo 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.gz
      • cd 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.

    0 讨论(0)
提交回复
热议问题