Running Python scripts under cPanel is fairly simple – actually pretty much the same as running anything that can be executed with CGI.
First thing’s first, your python script needs to know its interpreter (this will allow you to make your python scripts executable via the command line too, by the way).
Add this line to the top of your script:
#!/usr/bin/python
This is called a “hash bang”. It’s used to tell the caller what executable should be used to run the code in the file. In this case, that file is /usr/bin/python, the python interpreter.
Note: Run the command “whereis python” to double check the path of the python interpreter.
You’ll also need to make your script executable. Run this command:
chmod +x /home/username/public_html/cgi-bin/file.py
Your python file should be in your public_html/cgi-bin directory. If you would like the script to run outside the cgi-bin directory, place the following content into an .htaccess file in the directory you’d like to use:
Options +ExecCGI AddHandler cgi-script .py