1

I am getting ImportError: No module named 'mysql' while I do the following...

>>> import mysql.connector

MySQL is installed and am on Python 3.5. I can't figure out. The above command is running fine in Python 2.7.

2
  • 1
    Did you install a MySQL Connector for Python? Commented Nov 18, 2015 at 9:44
  • 1
    did u ever figure out the solution? checked the website and there is no connector for python 3.5 only 3.4 and below Commented Jan 5, 2016 at 19:32

4 Answers 4

2

Unfortunately there is no mysql-connector available for python3.5.

So, you can use pymysql module which is a replacement for mysql-connector package

pip install pymysql

import pymysql
Connection = pymysql.connect(host='hostname', user='username', password='password', 
             db='database',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor )
ExecuteQuery(Connection)
Connection.close()
Sign up to request clarification or add additional context in comments.

Comments

2

For me (OS X 10.11.4, Python 3.5.1), the mysql-connector-package installed itself (by default) into Python 2.7 that was also on my system. I copied the mysql package directory from: /Library/Python/2.7/site-packages/mysql to /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mysql. Everything works. According to http://dev.mysql.com/doc/connector-python/en/connector-python-versions.html the connector package supports Python 3.3+.

Your specific installation path for the module may be different, but you can easily check this from the REPL using the sys.path: https://leemendelowitz.github.io/blog/how-does-python-find-packages.html

1 Comment

I copied mysql folder from /Library/Python/2.7/site-packages/mysql right into site-packages of my virtualenv running Python 3.5. Works like a charm. Thanks.
1

As we need to be connecting MySQl with Python, the Python command assumes that the connector is enabled. So we need to follow the steps below:

  1. open MySQL Installer;
  2. go to "Custom settings";
  3. go to "Connectors";
  4. choose the connector\Python and version based on the one you are using;
  5. add it;
  6. use import mysql.connector.

It should run, in my case it did!

1 Comment

Please be aware that you are answering a question that is over 2 years old so it may not get a response. One of the comments to the question by Manny265, mentions that the connector is not available on python 3.5, only 3.4 and below. Have you checked and confirmed your answer works with python 3.5?
0

From Can't run MySql Utilities:

MYSQL Utilities assumes that the MySQL Connector for Python has been installed. If you install it (http://dev.mysql.com/downloads/connector/python/), MySQL Utilities should run OK.

I think this link may help you to solve your problem.

Comments

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.