[Solved] Python 3.4.0 with MySQL database

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?

Solution #1:

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.

Respondent: Andrew Gorcester

Solution #2:

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.

Respondent: lxx

Solution #3:

Install pip:

apt-get install pip

For acess MySQL from Python, install:

pip3 install mysqlclient
Respondent: Eduardo Basílio

Solution #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

Respondent: Steve R.

Solution #5:

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!

Respondent: MinasA1

Solution #6:

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

Respondent: SanD

Solution #7:

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…

Respondent: user1656671

Solution #8:

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

Respondent: AbsoluteMSTR

Solution #9:

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

Respondent: Frozen Flame

Solution #10:

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()
Respondent: imonaboat

Solution #11:

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

Respondent: netcoms

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.