Python 3.4.0 with MySQL database
Question:
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?
Answers:
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.
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.
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
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
Alternatively, you can use mysqlclient or oursql. For oursql, use the oursql py3k series as my link points to.
Install pip:
apt-get install pip
For acess MySQL from Python, install:
pip3 install mysqlclient
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…
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
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()
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!
for fedora and python3 use: dnf install mysql-connector-python3
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?
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.
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.
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
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
Alternatively, you can use mysqlclient or oursql. For oursql, use the oursql py3k series as my link points to.
Install pip:
apt-get install pip
For acess MySQL from Python, install:
pip3 install mysqlclient
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…
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
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()
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!
for fedora and python3 use: dnf install mysql-connector-python3