views:

6206

answers:

10

I'm already proficient in PHP and Javascript, but for a project a client is insisting that it be done in Python instead. Its a good opportunity, so I've agreed to learn Python.

My questions are:

  • How easy/hard will it be to build AJAX powered web applications in Python compared to PHP
  • what are the main differences in the two languages?
  • Any good resources for learning web development in Python?

All other information will of course be helpful as well.

+6  A: 

For web development I recommend using Django which is a Python Web framework

+1  A: 

This article on Python (and Django) for PHP developers should answer your questions.

RexE
+9  A: 

Here is a good comparative of both languages:

I like it because the comparison is more based on core language features than web frameworks.

CMS
biased comparison
ilhan
+6  A: 

Learn how to build WSGI applications. Your client will be disappointed if you build it using just pure CGI. Here is an example WSGI app:

#!/usr/bin/python
# An example Python WSGI application
#
from wsgiref.simple_server import WSGIServer, WSGIRequestHandler

def example_app(environ, start_response):
    # Environment variables are in `environ`
    # `start_response` is a function for passing the status and headers

    headers = []
    headers.append(("Content-type", "text/html"))
    start_response("200 OK", headers)

    return ["This is some data. The app must return an iterable like a list."]

httpd = WSGIServer(('', 8080), WSGIRequestHandler)
httpd.set_app(example_app)
httpd.serve_forever()

The benefit of WSGI being that it doesn't need to spawn another Python process for each request like CGI does.


According to the WSGI wiki,

WSGI is the Web Server Gateway Interface. It is a specification for web servers and application servers to communicate with web applications (though it can also be used for more than that). It is a Python standard, described in detail in PEP 333.

For more information, consult PEP-333.

Evan Fosmark
Added an explanation. I guess you have some reading to do. ;-) I highly suggest reading all of PEP-333. Also, look around the WSGI wiki to make sure you get it.
Evan Fosmark
Sounds good! If I have a server that runs cgi what do I need to do to get it to run wsgi?
monkut
monkut, WSGI typically isn't meant to be run through a server like Apache, but mod_wsgi does work. You should just run it using a WSGI server like the above script does. The above example starts a server that listens on port 8080. Run it and navigate to http://localhost:8080 in your browser.
Evan Fosmark
whats the advantage/difference of wsgi over cgi?
Click Upvote
Did you read the answer above? CGI means you have to fork a process for every request, whereas with WSGI like FastCGI, you don't.
Keltia
+3  A: 

As has already been mentioned, Django is certainly a popular choice for quick-and-easy MVC Python web apps. Other popular frameworks include:

Wikipedia also has a handy web framework comparison chart which includes Python web frameworks.

One thing that some people dislike about Turbogears, Django and the other higher level frameworks is that they encourage the use of specific associated technologies, such as one specific DB (MySQL or Postgres usually), one specific presentation template library (Kid / Cheetah etc.), or one Javascript library (usually MochiKit - but that is a good thing IMHO because it is designed to be "Pythonic" so it is usually the natural choice for Python web apps).

Personally I like CherryPy. It gives "just what you need" to web-enable Python scripts, like browser-based HTTP URL handling (mapping to functions / classes etc.), and even comes with an embedded web server (you can also plug it into the likes of mod_python, IIS or pretty much any other popular web server). You can then go ahead and use any other Python technologies around that which you desire, in any combination. Of course the usual case of extra flexibility giving you extra opportunity to give yourself future hassles applies here.

Wayne Koorts
My understanding, at least with Django, is that they allow: 1) MySQL, Postgres, SQLite, and Oracle on the database site, and 2) they allow you to use other template library (besides the framework specific one): http://tinyurl.com/7ctplq. As far as I know, it also does not require a JS library
Rob
+1  A: 

I'd throw in Beginning Python into the mix as far as learning Python itself. Many other resources online, but if you need an out right start in the language.

Rob
For good online tutorials for learning Python there is the official Python tutorial written by Python's creator (link on Python.org) and also diveintopython.org
Wayne Koorts
+5  A: 

Dive Into Python is an excellent introduction suitable for programmers already familiar with other languages.

One of the important differences that might trip you up at first is that Python is strongly typed. This means that integers and strings are distinctly different, and there are no automatic conversions between the two. The str() and int() functions may become helpful (but remember that int() may throw an exception if you give it something that doesn't look like an integer!).

Greg Hewgill
Python isn't strongly typed in the same sense as C or Java, though - variables themselves don't carry type information. Only their values do.
David Zaslavsky
Dive Into Python is a little old and not updated. I think it is a great read, but something else should also be read like Python in a Nutshell by Alex Martelli or the Python Cookbook
apphacker
@David: You're thinking of statically typed, which is different from strongly typed. In particular: http://diveintopython.org/getting_to_know_python/declaring_functions.html#d0e4188
Greg Hewgill
@apphacker Try Dive Into Python 3... Released this month! (http://diveintopython3.org)
Beau Martínez
+2  A: 

To learn the language, i would suggest to start with the book "A byte of Python" and later on "Dive into Python" as some one above has already suggested you. Well! i use Django framework to develop web application. It's really cool and fast too.

aatifh
A: 

Since Django was recommended here, I feel the need to also mention Zope.

Glenn
+1  A: 

Things to watch out for

  • In python, everything in the same scope goes into the same namespace (variables, functions, classes), so it's possible to wipe out a class by creating a function of the same name within the same module.

  • In python, lists and dictionaries (the equivalent of PHP's arrays) are copied by reference. Make sure you copy them manually or you will be modifying the original.

  • The Python documentation is not as easy to navigate as PHP's manual.

too much php
Point 1 is incorrect - I assume you meant to write PHP?
Wayne Koorts
I did mean Python, hopefully it's clearer now
too much php
OK yes that makes sense now. By the way point number 3 is subjective. I find the Python docs very easy to navigate.
Wayne Koorts
I was used to typing "php.net/function_name" into my address bar, I don't think it gets much easier than that.
too much php
Well "easy navigation" can easily be a whole endless discussion on its own, but of course if you want to know what a function that you know about does then that is a great way to find out the details of it.
Wayne Koorts