I want to connect to MySQL with Python3.

Error message

At the import stage of the very first line
ImportError: No module named'MySQLdb'has occurred.

It is done after completing pip install as above site

Traceback (most recent call last):
  File "test_MySQL3.py", line, in<module>
    import MySQLdb
ImportError: No module named 'MySQLdb'
Applicable source code
import MySQLdb
conn = MySQL.connect (host = 'localhost', db = 'test2', user = 'hoge', passwd = 'pass', charset = 'utf8')
cursor = conn.cursor ()

1.pip list I checked mysqlclient (1.3.13).
2. I tried sudo apt-get –-purge autoremove mysqlclient,
Loading package list ... Done
Creating dependency tree
Reading status information ... Done
E: Package mysqlclient could not be found. I think there is a problem around here.

Supplemental information (FW/tool version etc.)

Python 3.5.3
Raspberry Pi 2 model B

  • Answer # 1

    A reliable solution.

    $python3 -m pip install mysqlclient

    Please write your interpreter command in python3. It may be python or python3.5. You need to write the command that the questioner uses to run the program.

    The path of the command such as pip3 probably passes, so if you can find the command that has the substance in the same place as the interpreter you are using (use the which command etc.), then use that Is good.

    pip is actually a python module.

    Because it is completely independent of system package management, it doesn't relate to apt, nor does it manage multiple python environments. It is associated with the environment of the specific python interpreter itself.

    It is a simple system once you know it, but it seems to be a little difficult until you get used to it.