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?
This question is related to
python
mysql
python-3.x
mysql-python
python-3.4
Install pip:
apt-get install pip
For acess MySQL from Python, install:
pip3 install mysqlclient
Alternatively, you can use mysqlclient or oursql. For oursql, use the oursql py3k series as my link points to.
for fedora and python3 use: dnf install mysql-connector-python3
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
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
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.
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...
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!
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()
Source: Stackoverflow.com