17

After I installed MySQLdb by

sudo apt-get install python-mysqldb

I can import and use it when I use Python 2.6, but not 2.7. (ImportError: No module named MySQLdb)

I think that apt-get install MySQLdb in version 2.6, but I don't know how to make python 2.7 work with it. Please help me. Thanks a lot!

iForests
  • 6,013
  • 9
  • 34
  • 70

1 Answers1

29

If you want system-wide installation for python2.7 you should use easy_install-2.7. Install setuptools (or distribute) for python 2.7, then type:

easy_install-2.7 mysql-python

Update:

Or just

sudo apt-get install python2.7-mysqldb

Notice without adding sudo you'd not be able to make the changes.

Oke
  • 113
  • 8
Maksym Polshcha
  • 16,722
  • 8
  • 47
  • 73
  • 6
    [pip](http://www.pip-installer.org/) is the successor to easy_install and a bit [more modern](http://stackoverflow.com/questions/3220404/why-use-pip-over-easy-install). – miku Apr 08 '12 at 10:18
  • @miku Personally I prefer distribute. It's better then pip for me. – Maksym Polshcha Apr 08 '12 at 10:19
  • 1
    AFAIK that requires having python2.7-dev and all the compiler stuff, plus libmysql, otherwise you won't be able to compile it. but I have installed this on both python 2.6 and 2.7 with apt-get only, trouble is I don't remember how, if anything was needed for it. IIRC installing python-mysqldb installed it for both python versions. – Not_a_Golfer Apr 08 '12 at 10:19
  • I can't install mysql-python by pip, here is the error message: http://dl.dropbox.com/u/17350105/error Please help... Thanks! – iForests Apr 08 '12 at 10:22
  • Thanks, @MaksymPolshcha, but I got "E: Couldn't find package python2.7-mysqldb" – iForests Apr 08 '12 at 10:25
  • @陳碩甫 To install using pip you have to install python2.7-dev and mysql-dev (or libmysql-dev depending on your OS) – Maksym Polshcha Apr 08 '12 at 10:32
  • I am using Ubuntu 10.04 LTS. I installed mysql-python successfully after installed python2.7-dev. You save my life. Thank you really!! – iForests Apr 08 '12 at 13:03
  • Danke @MaksymPolshcha "sudo apt-get install python2.7-mysqldb" has worked for me. – Triumf Maqedonci May 26 '20 at 11:37