1

I need to use a MySQL Database in my Python program, so i need to connect Python to MySQL.

I am using MacOsX Sierra and i tried installing connector, mysqlclient and nothing worked for me.

I use PyCharm, and whenever I type

import MySQLdb

or

import mysql.connector

I get the error:

Traceback (most recent call last):
  File "/Users/muhammed/Desktop/PyhtonHomework/venv/homework.py", line 1, in <module>
    import MySQLdb
ModuleNotFoundError: No module named 'MySQLdb'

How can I fix it? I have a term project and i can't even start thanks to this error.

Python version: 3.6.4

8
  • You should post the actual traceback. "Mysql" != "mysql" so the error, as you stated it, doesn't match your code Commented Mar 18, 2018 at 15:09
  • I edited the post with traceback. Commented Mar 18, 2018 at 15:17
  • It's case-sensitive. You should try import mysql instead. The first attempt is obviously wrong but the second attempt, if it throws the same error, is not so clear. You did pip install the module, right? Commented Mar 18, 2018 at 15:22
  • Thanks for reply. I tried running 'import mysql' and it still prints the same traceback with ModuleNotFoundError: No module named 'mysql'. Commented Mar 18, 2018 at 15:25
  • Yes i installed every required thing with pip install. Commented Mar 18, 2018 at 15:26

1 Answer 1

5

MySQLdb doesn’t support Python 3. Try using an alternative MySQL Python package.

I successfully used MySQL Connector Python, specifically http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.zip with Python 3 — have a look at the latest version and the usage documentation.

pip install http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.zip

then

import mysql.connector

Using the latest version available (I believe 2.1.7).

Sign up to request clarification or add additional context in comments.

11 Comments

Hi, thanks for your answer. I already tried installing and importing mysql.connector but Pycharm still gives me ModuleNotFoundError: No module named 'mysql'.
Can it be caused by the installation path or someting?
Which package are you trying to install exactly? What command are you running?
i wrote to terminal: pip install mysql-connector-repackaged and it was installed successfully.
Click PyCharm > Preferences > Project > Project Interpreter. Select the correct interpreter (ideally use a virtual environment). You can see all your packages installed for the current project here, and press the + to add a new one. Once you're using the correct interpreter, you should be able to pip install too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.