9

I have been trying to use MySQL in a Python project I've been working on. I downloaded the connector: mysql-connector-python-2.1.3-py3.4-winx64 here.

I already had Python 3.5.1 installed. When I tried to install the connector, it didn't work because it required python 2.7 instead. I have searched on many sites, even on StackOverflow I couldn't find a solution.

Thanks for any help.

linusg
  • 6,289
  • 4
  • 28
  • 78
Awa Melvine
  • 3,797
  • 8
  • 34
  • 47
  • 1
    Try [`pymysql`](https://pypi.python.org/pypi/PyMySQL). It is compatible with Python 3. You can install it with pip: `pip install pymysql`. – Gustavo Bezerra Feb 28 '16 at 15:48

7 Answers7

16

I did the steps below with Python 3.5.1 and it works:

  • Download driver from here
  • Driver installation in cmd, in this folder Python\Python35\PyMySQL-0.7.4\pymysql

    python setup.py build
    python setup.py install
    
  • Copy folder Python\Python35\PyMySQL-0.7.4\pymysql to Python\Python35\pymysql

  • Sample code in python IDE

    import pymysql
    import pymysql.cursors
    conn= pymysql.connect(host='localhost',user='user',password='user',db='testdb',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
    a=conn.cursor()
    sql='CREATE TABLE `users` (`id` int(11) NOT NULL AUTO_INCREMENT,`email` varchar(255) NOT NULL,`password` varchar(255) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;'
    a.execute(sql)
    
  • Enjoy It!
chinnychinchin
  • 5,564
  • 2
  • 21
  • 18
Soheil
  • 473
  • 9
  • 23
16

I am using Python 3.5.2 on window 8 pro 64-bit and the following procedure is worked for me.

  1. Download driver (PyMySQL-0.7.9.tar.gz (md5)) from here

  2. Extract and copy the folder pymysql into the python Lib folder e.g (C:\Users\MyUsername\AppData\Local\Programs\Python\Python35-32\Lib)

  3. Copy and run the following example.py
#!/usr/bin/env python

import pymysql

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='sandbox')

cur = conn.cursor()
cur.execute("SELECT * FROM users")

print(cur.description)
print()

for row in cur:
    print(row)

cur.close()
conn.close()

I hope it will work for you as well. Happy coding :)

MasoodRehman
  • 715
  • 11
  • 20
4

Try this link: MySQL - Downloads - Connector - Python

From Select Platform, select the platform independent an download MySQLconnector.

After extracting the file go to its directory where setup.py is located.

WINDOWS: press shift + right_click and open command windows and type:

python setup.py install`
majid zareei
  • 723
  • 6
  • 10
  • Brilliant! This worked for me on Windows with python3.5 when I couldn't find any other way to get mysql package (dependency for sqlalchemy) – user3450049 Feb 18 '17 at 22:07
  • I had to start the power shell as Administrator to be able to install the package to site-packages. It worked then using Python 3.6. – romor May 24 '17 at 21:41
3

Visit this web site and you will find a mysqld package that works fine with Python 3 on Windows : http://www.lfd.uci.edu/~gohlke/pythonlibs/

Otherwise you can use pymysql which might be slower but works fine with Python 3.

Alex C.
  • 166
  • 3
  • 9
1

Use the mysqlclient library. Install with: pip install mysqlclient

It is a fork of MySQLdb ( which was formerly installed via pip install mysql-python) that supports Python 3.*

This library talks to the MySQL client's C-interface, and is faster than the pure-python pymysql libray.

*Note: you will need the mysql-developer tools installed. An easy way to do this on a Mac is to run brew install mysql to delegate this task to homebrew. If you are on linux, you can install these via the instructions at the mysqlclient github page.

The Aelfinn
  • 13,649
  • 2
  • 54
  • 45
1

In Windows, I used:

pip3 install pymysql
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
fizux
  • 11
  • 1
0

I was facing mysql database connection problem with Windows, Python 3.5.2 and Django. But finally I resolved it by installing "mysqlclient‑1.3.9‑cp35‑cp35m‑win32.whl" from http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient

Download the whl file, then enter into same directory in command prompt and run the below command.

pip install mysqlclient-1.3.9-cp35-cp35m-win32.whl

Note: Python 3.5.2 does not have official support for MySQL yet, so its just un-official binary to over come this problem for now.

Hope that will help you !!!