Setting up Python and PyMYSQL on your MAC | Run Python on XAMPP or Apache

1. Download and Install XAMPP – make sure MySQL and Apache is running

2. Python – download and install it at
3. Edit http.conf in your apache. Find: AddHandler cgi-script .cgi .pl .asp and add .py and save
4. Instal PIP in your machine

– download

Copy the code and put it somewhere in your computer.. and then go to file location via terminal.. then run following command:

sudo python

5. Then lastly install PyMYSQL

type this command: pip install PyMysql

6. Then restart your apache and Mysql

7. Create python file cgi-bin/hello/
cgi-bin folder is inside the XAMPP


PHP was designed for web programming so it automatically attaches Content-type to HTTP headers but Python doesn't. Prepend this to main():

print "Content-Type: text/html\n"

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *