try running the following command:
pip install mysqlclient
Answer from Ortal Blumenfeld Lagziel on Stack OverflowHow can I connect to MySQL in Python 3 on Windows? - Stack Overflow
Integrating MySQL with Python in Windows - Stack Overflow
How to install MySQL Connector for Python on Windows when according to the installation manager Python is not installed? - Stack Overflow
Why I am unable to create connection with MySQL using python on Window 11 OS
There are currently a few options for using Python 3 with mysql:
https://pypi.python.org/pypi/mysql-connector-python
- Officially supported by Oracle
- Pure python
- A little slow
- Not compatible with MySQLdb
https://pypi.python.org/pypi/pymysql
- Pure python
- Faster than mysql-connector
- Almost completely compatible with
MySQLdb, after callingpymysql.install_as_MySQLdb()
https://pypi.python.org/pypi/cymysql
- fork of pymysql with optional C speedups
https://pypi.python.org/pypi/mysqlclient
- Django's recommended library.
- Friendly fork of the original MySQLdb, hopes to merge back some day
- The fastest implementation, as it is C based.
- The most compatible with MySQLdb, as it is a fork
- Debian and Ubuntu use it to provide both
python-mysqldbandpython3-mysqldbpackages.
benchmarks here: https://github.com/methane/mysql-driver-benchmarks
You should probably use pymysql - Pure Python MySQL client instead.
It works with Python 3.x, and doesn't have any dependencies.
This pure Python MySQL client provides a DB-API to a MySQL database by talking directly to the server via the binary client/server protocol.
Example:
import pymysql conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql') cur = conn.cursor() cur.execute("SELECT Host,User FROM user") for r in cur: print(r) cur.close() conn.close()
» pip install mysql-connector-python
Download page for python-mysqldb. The page includes binaries for 32 and 64 bit versions of for Python 2.5, 2.6 and 2.7.
There's also discussion on getting rid of the deprecation warning.
UPDATE: This is an old answer. Currently, I would recommend using PyMySQL. It's pure python, so it supports all OSes equally, it's almost a drop-in replacement for mysqldb, and it also works with python 3. The best way to install it is using pip. You can install it from here (more instructions here), and then run:
pip install pymysql
This may read like your grandpa givin advice, but all answers here did not mention the best way: go nd install ActivePython instead of python.org windows binaries. I was really wondering for a long time why Python development on windows was such a pita - until I installed activestate python. I am not affiliated with them. It is just the plain truth. Write it on every wall: Python development on Windows = ActiveState!
you then just pypm install mysql-python and everything works smoothly. no compile orgy. no strange errors. no terror. Just start coding and doing real work after five minutes.
This is the only way to go on windows. Really.
» pip install mysqlclient
» pip install MySQL-python
(Edit: Windows 10 64-bit)
Hello. I am trying to learn how to develop web applications. I understand HTML/CSS/Javascript decently and have recently finished a few Python tutorials. My goal is to have a basic front-end that allows me to create an account and perform some CRUD operations in a database. i have already installed mySQL and create a few tables. I originally installed Python 3.6.1; however, found that the recommended mySql library wouldn't install when trying to run the .msi file. No errors were given. It attempts to start the installer but it crashes after a few milliseconds. I've unistalled 3.6.1 and have installed every other version of python i can find but can't get this to work. I also ensure that I'm trying to install the correct connector/driver for the appropriate python version, to no avail. Is there a simple path to follow in order to download the correct version of python needed for this basic functionality and the appropriate library to connect to mysql? Any and all insight would be greatly appreciated.
Thanks in advance!
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 installCopy 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!
I am using Python 3.5.2 on window 8 pro 64-bit and the following procedure is worked for me.
Download driver (PyMySQL-0.7.9.tar.gz (md5)) from here
Extract and copy the folder pymysql into the python Lib folder e.g (C:\Users\MyUsername\AppData\Local\Programs\Python\Python35-32\Lib)
- 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 :)