anyway, just dipping my toes into python… what is python good and bad for?
python is a general purpose programming language, which can be used to do anything. Because of it’s clean syntax, great libraries, and the numerous python books (many recently published), I highly recommend python. Famously, google hired the top python gurus to work for them because google uses python extensively in house.
would you use it instead of php for example to do webpages with a bit of functionality?
Yes. In addition, there are also python ‘frameworks’ available for more complex websites.
if i wanted to use python like php, for webpages etc., how would the python file be called?
I think it depends on the server. For instance, with Apache set up for local development on my pc (which is something everyone should have set up), the url I use to run a python script that uses cgi (cgi is used to communicate with the server) is:
Here is the script:
import cgitb; cgitb.enable()
print "Content-type: text/html"
print "<h1>Hello World</h1>"
You need a ‘shebang’ line at the top of myprog.py.
You need to change the permissions for the file myprog.py to give everyone execute privileges, e.g.
$ chmod a+x myprog.py
- Put the file in the cgi-bin directory on the server, e.g. /Library/Apache2/cgi-bin
i don’t want urls xyz.com/cgi-bin/whatever.cgi. how’s that usually handled?
Servers, like Apache, allow you to map fake urls like:
to real urls like:
In addition, python frameworks provide additional ways to map urls, an example is here:
If you’re running Apache you might want to check out mod_python which is basically a replacement for cgi (which is slow).
Setting up mod_python and figuring out how to call scripts is a bit of a pain, but if your host already has it setup, it is well worth learning. If you are going to use mod_python on your host, then you should bite the bullet and set it up for local development on your pc, too.
Python is not a language that tries to be the fastest (pythonistas do not seek excessive optimisation)
That isn’t true. python is a language that has been developed with an eye on the speedometer. All the ‘p’ languages along with ruby, compete for users–and speed is a major selling point. Furthermore, python allows you to identify bottlenecks in your code, so that you can opt to rewrite those portions in an even speedier language like C\C++, and then call those functions from your python program. In addition, on discussion forums the efficiency of various solutions is always discussed.
ruby is the language that plays down optimizations/speed–presumably because it can’t compete with python’s speed, so the rubyist’s fall back motto is: if you need speed, you probably don’t, but if you really do, then write the code in C–not ruby. There are plenty of speed tests posted around the internet, so you can decide for yourself where python stands.
Because sitepoint puts no effort into any language except php, the best python discussion forum resides here: