54

I have installed Python version 3.4.0 and I would like to do a project with MySQL database. I downloaded and tried installing MySQLdb, but it wasn't successful for this version of Python.

Any suggestions how could I fix this problem and install it properly?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Srdan Ristic
  • 3,203
  • 5
  • 18
  • 23

11 Answers11

79

MySQLdb does not support Python 3 but it is not the only MySQL driver for Python.

mysqlclient is essentially just a fork of MySQLdb with Python 3 support merged in (and a few other improvements).

PyMySQL is a pure python MySQL driver, which means it is slower, but it does not require a compiled C component or MySQL libraries and header files to be installed on client machines. It has Python 3 support.

Another option is simply to use another database system like PostgreSQL.

Community
  • 1
  • 1
Andrew Gorcester
  • 19,595
  • 7
  • 57
  • 73
  • I tried also with pymysql but still nothing. I will try with git repo and see if I can get it to work, otherwise I am just going to re-install 3.4 and install 2.7 version... – Srdan Ristic Apr 29 '14 at 22:18
  • 1
    Can you be more specific about what went wrong with pymysql? – Andrew Gorcester Apr 29 '14 at 22:27
  • 1
    I actually just got it to work, I had some problems with pip installer tool, that's why I wasn't able to install anything but I fixed it, MySQLdb is still not working even though I installed it properly this time, but then and I ran **pip install PyMySQL** and it's all working good with PyMySQL my code is showing everthing from my MySQL database, thank you so much for the answer! – Srdan Ristic Apr 29 '14 at 23:14
  • Great, glad that worked out! If you are having trouble with `pip` I recommend looking into using a `virtualenv` and running all pip commands inside the `virtualenv` as a user (not as root/with `sudo`) – Andrew Gorcester Apr 30 '14 at 03:27
  • Andrew could you please give me your e-mail so I can contact you in order to get some advises about pymysql, because I want to learn it little bit more, for example I would like to use it with Flask but I am not really sure how to do it so if you could help, if you know it? – Srdan Ristic Apr 30 '14 at 16:00
  • I can be reached at firstname.lastname@gmail, but I'm no expert on flask specifically. I do know that flask uses an ORM called SQLAlchemy, which is much much nicer than bare SQL. So I'd recommend starting with the flask database documentation and using the SQLAlchemy pymysql connector for that. – Andrew Gorcester Apr 30 '14 at 16:55
  • still have django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb' – madzohan Apr 22 '15 at 09:24
  • 1
    @madzohan Django documentation now recommends you use mysqlclient as the primary MySQL database driver. Try installing that instead and see if it works. If it doesn't (perhaps because your version of Django is old?), or if you want to use pymysql anyways, refer to the second answer on this page: http://stackoverflow.com/questions/7224807/what-is-pymysql-and-how-does-it-differ-from-mysqldb-can-it-affect-django-deploy – Andrew Gorcester Apr 22 '15 at 19:06
  • @AndrewGorcester `it is not the only MySQL driver for Python` You are right, so for completeness your answer really should mention [Oracle's MySQL/Python connector](http://dev.mysql.com/doc/connector-python/en/) which is really well documented, as well as any pros and cons you perceive for that adapter to help future readers weigh pros and cons. The answer ranks high on Google so that investment would be worthwhile it for the community. Thanks! – zx81 Jul 15 '15 at 23:13
  • To use *mysqlclient* in Python 3 and above in your python code add at the top *import MySQLdb* It was not that obvious for me. – Joseph Oct 29 '17 at 18:32
22

Use mysql-connector-python. I prefer to install it with pip from PyPI:

pip install --allow-external mysql-connector-python mysql-connector-python

Have a look at its documentation and examples.

If you are going to use pooling make sure your database has enough connections available as the default settings may not be enough.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
lxx
  • 1,326
  • 20
  • 30
  • 1
    For anyone who is not aware, this is the adapter made by Oracle (which now owns MySQL), so it should be pretty "close to the database". It works with 3.4, has great documentation, and provides a number of useful methods and attributes not found in PEP249. (Is that a good thing? It depends on whether you care about extracting the most juice out of your database, or on whether you want to stay close to PEP249 to make it easy to switch databases later.) They have announced a C version (2.1) but haven't said when it will be out. – zx81 Jul 15 '15 at 23:08
  • zx81 one bonus with mysql-connector-python is no dependencies and doesn't require mysql or mysql-client to be installed which is handy for keeping installs small – lxx Jul 20 '15 at 02:11
  • 3
    I can't install it with `pip`... *No distributions at all found for mysql-connector-python*. – Pieter Dec 31 '15 at 12:33
  • Try pip install --allow-external mysql-connector mysql-connector source code for it is at https://github.com/mysql/mysql-connector-python – lxx Aug 06 '16 at 08:04
  • 2
    Unfortunately this doesn't seem to work anymore, at least using pip 8.1.2 on python 3.5. I get this error `DEPRECATION: --allow-external has been deprecated and will be removed in the future. Due to changes in the repository protocol, it no longer has any effect.` – John Cummings Sep 02 '16 at 11:32
  • 1
    yes no longer works. I don't know a replacement as haven't worked with mysql this year only postgresql/redshift. – lxx Sep 18 '16 at 01:09
  • alternative installation methods https://dev.mysql.com/doc/dev/connector-python/8.0/installation.html – lxx Aug 24 '19 at 10:35
10

Install pip:

apt-get install pip

For acess MySQL from Python, install:

pip3 install mysqlclient
4

There is a Ubuntu solution available either through the Ubuntu Software Center or through the Synaptic Package Manager. This will connect Python version 3.4.0 to MySQL. Download "python3-mysql.connector" version 1.1.6-1.

Note that the connection syntax does not use "MySQLdb". Instead read: Connecting to MySQL Using Connector/Python

Steve R.
  • 220
  • 2
  • 13
4
sudo apt-get install python3-dev
sudo apt-get install libmysqlclient-dev
sudo apt-get install zlib1g-dev
sudo pip3 install mysqlclient

that worked for me!

MinasA1
  • 164
  • 7
3

I solved it this way: download the zipped package from here and follow this set of instructions:

unzip  /path/to/downloads/folder/mysql-connector-python-VER.zip  

In case u got a .gz u can use ->

tar xzf mysql-connector-python-VER.tar.gz 

And then:

cd mysql-connector-python-VER  # move into the directory

sudo python3 setup.py install # NOTICE I USED PYTHON3 INSTEAD OF PYTHON

You can read about it here

SanD
  • 473
  • 7
  • 11
2

It seems that at the moment Ubuntu 15.10 has a but with python3 and pip3.

As elaborated in this article.

The problem makes pip3 install to python3.5 while python3 is actually running python3.4 :(

Until a proper solution will be available via the updates you can do one of the following:

run

python3 -m pip install pymysql

instead of

pip3 install pymysql

(or any other package)

Now

import pymysql

should work in python3 and in idle3.

Alternatively, if you explicitly need 3.5 you can use explicit python3.5 instead of python3. but idle3 will still point to 3.4...

QT-1
  • 900
  • 14
  • 21
1

mysqlclient is a fork of MySQLdb and can serve as a drop-in replacement with Python 3.4 support. If you have trouble building it on Windows, you can download it from Christoph Gohlke's Unofficial Windows Binaries for Python Extension Packages

1

Alternatively, you can use mysqlclient or oursql. For oursql, use the oursql py3k series as my link points to.

Frozen Flame
  • 3,135
  • 2
  • 23
  • 35
0

Maybe you can use a work around and try something like:

import datetime
#import mysql
import MySQLdb
conn = MySQLdb.connect(host = '127.0.0.1',user = 'someUser', passwd = 'foobar',db = 'foobardb')
cursor = conn.cursor()
HPierce
  • 7,249
  • 7
  • 33
  • 49
imonaboat
  • 29
  • 7
-1

for fedora and python3 use: dnf install mysql-connector-python3

netcoms
  • 1
  • 1