Saturday, September 4, 2010

Install MySQL 5 for Python 2.6 (and django) on Windows

I've heard a lot about python and his framework Django, But because i love php i never found the time to test python and honestly i didn't fell the need to do. untill this year (2010)  but the start was not very good. I installed puython django i did some tests. wow  thats amazing, very simple , now  i want to add mysql. I did some researches about mysql connectors i found Mysqldb but there is no binary distribution for Python 2.6 damn it!! thats the version i use.

It took me 4 days looking how to install this Mysqldb to no avail.  i found a lot of sulutions no one is working with me
I tried to build it from the source with many ways to no avail. My environment is Windows XP. MySQL 5.0. Python 2.6 (windows version).

Yesterday i received an email from a friend about another mysql connector named "pymysql" it's very easy to install it in three steps:

Install Python setuptools (easy_install.exe) if you haven’t installed it. It is required to install pymysql.
To install setuptools
Download these two files and put them into the same directory:
Open a command prompt in this directory and type "python setuptools-0.6c9-py2.6.egg"

Add the path where easy_install is installed into your environment PATH ({PYTHON_PATH}\Scripts, {PYTHON_PATH} is the path where python is installed).
Install pymysql, it's easy just donwload the latest version of PyMySQL-0.3-py2.6.egg, put it in a directory and open a command prompt, change to this directory. then  run "easy_install.exe PyMySQL-0.3-py2.6.egg" if it doesn't work try an absolute path of easy-install ({PYTHON_PATH}\Scripts\easy_install.exe PyMySQL-0.3-py2.6.egg)
Now you can test it in python just open the "IDLE(Python GUI)" and run the test below:
>>>import pymysql
>>>conn = pymysql.connect(host='localhost', port=3306, user='root', passwd=None, db='mysql')
>>>cur = conn.cursor()
>>>cur.execute("SELECT Host,User FROM user")
>>>for r in cur:
>>>   print r 
Step 4 for django: 
To use it with django you need one more step:
go to the django directory, exactly in this directory
and simply do a search and replace of MySQLdb with pymysql
in the two files "" and  ""

That's it.

 thanks to mouad for the link and to Pete for this great lib.


bounmed said...

if you get some errors and warnings in step 2: it's ok. go to the next step.

Tom said...

you saved my life. praise you!

bounmed said...

My pleasure, anytime

YL said...

I have python2.7 and not sure how to install PyMySQL-0.3-py2.6.egg: I don't have easy_install.exe on my machine

But I got Django work on xp somehow (through django's tutorial)

So PyMySQL is not necessary?

bounmed said...

hi YL,
no it's not if you don't want to use mysql as a database.
but to use mysql database you need PyMySQL.

bounmed said...

hi YL,
For your first question about "easy_install.exe" look at step1

Ricardo said...

Thanks a lot for your explanation, im a newbie on python and this instructions helped a lot, it worked ok!!.

bounmed said...

@Ricardo, you are welcome, i'm happy it helped you

prabir said...

Hi bounmed.. nice article.. everything worked perfectly until i ran the command "python syncdb " It gave me errors.
File "", line 28, in
File "C:\sowo-env\Lib\site-packages\django\core\management\", line
352, in execute_from_command_line
File "C:\sowo-env\Lib\site-packages\django\core\management\", line
306, in execute
File "C:\sowo-env\Lib\site-packages\django\core\management\", line 192,
in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\sowo-env\Lib\site-packages\django\core\management\", line 219,
in execute
output = self.handle(*args, **options)
File "C:\sowo-env\Lib\site-packages\django\core\management\", line 348,
in handle
return self.handle_noargs(**options)
File "C:\sowo-env\Lib\site-packages\django\core\management\commands\"
, line 54, in handle_noargs
tables = connection.introspection.table_names()
File "C:\sowo-env\Lib\site-packages\django\db\backends\", line 405,
in table_names
return self.get_table_list(cursor)
File "C:\sowo-env\lib\site-packages\django\db\backends\mysql\"
, line 33, in get_table_list
cursor.execute("SHOW TABLES")
File "C:\sowo-env\Lib\site-packages\django\db\backends\", line 19, in e
return self.cursor.execute(sql, params)
File "C:\sowo-env\lib\site-packages\django\db\backends\mysql\", line 83
, in execute
return self.cursor.execute(query, args)
File "build/bdist.macosx-10.6-universal/egg/pymysql/", line 102, in
File "build/bdist.macosx-10.6-universal/egg/pymysql/", line 175,
in defaulterrorhandler
pymysql.err.InternalError: (1046, u'No database selected')
earlier i made changes in and had some database name and it gave different error saying database does not exist or something like that. that why i removed it. How should I make a database when playing with pymysql. I am really confused.Please help me out.

jrp said...

Thank you :)

Anonymous said...

awesome, thanks so much for posting this

prem said...

I get this error,

In step 1, you have asked to save 2 files..what would be the filenames of both those files..

2nd file I have named it as,

what would be my 1st file name.

C:\Python27\Lib\site-packages>python setuptools-0.6cc9-py2.6.egg
Searching for setuptools-0.6cc9-py2.6.egg
Couldn't find index page for 'setuptools-0.6cc9-py2.6.egg' (maybe misspelled?)
Scanning index of all packages (this may take a while)
No local packages or download links found for setuptools-0.6cc9-py2.6.egg
Best match: None
Traceback (most recent call last):
File "", line 278, in
File "", line 239, in main
File "C:\Python27\Lib\site-packages\setuptools\command\", line
1712, in main
File "C:\Python27\Lib\site-packages\setuptools\command\", line
1700, in with_ei_usage
return f()
File "C:\Python27\Lib\site-packages\setuptools\command\", line
1716, in
distclass=DistributionWithoutHelpCommands, **kw
File "C:\Python27\lib\distutils\", line 152, in setup

Anonymous said...

Hi, Thanks a lot
It helped a lot, I was trying from last three days.
Great job,thank u again.

Haafiz said...

I also have a blog and I have also installed Django and used your way to use MySQL with it so I am writing a post on installing Django on windows and using it with MySQL so in that post I am referring to your post and using your steps to tell about installing pymysql. Hope you will not mind.