I have seen no website built by Python such that the url of the website contains .py at the end. I have seen only index.html and index.php, but sometimes nothing as in SO. I know that you can hide the end somehow by .htaccess -file.

I would like to know the reason for that.

Why is there no website ending with .py?


Typically PHP scripts are accessed directly, similarly to a CGI script - basically you access the script like any regular file, but the web server intercepts this request, runs the script and returns the output instead of the file contents.

Most Python frameworks have their own routing systems, where you define something like..

urls = (
    ('^/article/(\d+)$', ArticleController),
    ('^/user/(.+?)$', UserController)

..then you would access something like..


..and it would be run through the URL mapping defined in urls, and send the request to the appropriate class... but having the router.py part in all your URLs is a bit crappy, so you use mod_rewrite to redirect /(.*) to /router.py/$1 (either via .htaccess, your Apache configuration etc)

You can absolutely do the same thing with PHP, or any other language - this is exactly how CodeIgniter works (a PHP framework)

If you are utterly insane, you could use mod_rewrite and map /user/(\d+).py to /ViewController.php?id=$1 or something, but there's generally no point to adding a fake extension to the page (there's a few exceptions, mainly backwards compatibility, and allowing access to data in different formats in an API)

You can also do the "PHP way" using Python, if you write each page as a separate CGI script, just most Python frameworks tend to use the MVC (or MVC-like) setup I described above..


A good URL would be independent of technologies, so a .php or .py or .rb extension is something to be prevented. Mostly, python frameworks(like Turbogears) handle all the URL parsing and the python program that runs in the background gets all its parameters in a nice data structure, so you don't see any file extension in that case.

See "cool URIs don't change" by Tim Berners-Lee


Well, probably most people hide the extension because it is the smarter way to go. It means if you say, switched to PHP (not that you would want to...), you wouldn't have to change the extensions.

However, there are site with .py extensions (several parts of Google have it).

The better questions to ask is, why do most ASP.NET and PHP users insist on keeping the extenstions?

  • Your answer suggests me that it is easier to get rid of the extension in Python than in PHP and ASP.NET. --- Which Python module does provide the functionality? Jul 26 '09 at 19:53
  • 2
    @Masi: There's no module that does something specific to remove the extension. In fact in Python there is no extension to be gotten rid of. To put it another way, the extension in the URL does not need to correspond to the extension of the file. So if your web app is made of .py files, there is absolutely no reason your URLs need end in .py. This might not make sense if you're used to PHP, since PHP forces the URL's extension to be the same as the file's extension (well, you can even work around that but it's not especially straightforward); it's the same in ASP I presume.
    – David Z
    Jul 26 '09 at 23:04

What is transfered to your computer is HTML (possibly with CSS and JavaScript code). CSS and JavaScript aren't displayed per se, so you don't see those extensions. HTML is the content to be displayed, so you're used to seing .html extension.

Now, who cares if the HTML document was written manually in Notepad, or dynamically put together with some programs that run just 0.1 second ago. That's no difference.

PHP is often used to create HTML documents on-the-fly this way. Adding .php extesion to HTML documents that were created by a PHP program is customary and doesn't mean much. The end user sees a HTML document in the browser.

Web server needs some information how to find a HTML document to serve - read a hand-made HTML document from disk? Create one by running some PHP program? .php extension can be used as a hint. That's only a server configuration matter.

If you are interested in serving the Web with python, look at project django


Django, the (probably) most popular framework for doing webapps/websites in python, uses url-mapping to python functions, so when you request /url/ it will actually call a function that's assigned to handling requests to that url. In other words, there's not a 1-to-1 mapping between urls and .py files.

Update (per comment):

In a typical php website, the url would look something like /path/to/file.php and upon this request, the webserver runs the php file and returns its output as the HTTP Response.

In Django, a url of the form /path/of/url/ is intercepted by the framework, it doesn't actually correspond to anyfile, it's a simple a part of the HTTP Request, and the Django framework has a url-dispatching mechanism that maps a url to a normal function. If it finds a function that's assigned to handle this type of url, it will call it, and return the Http Response that the function returned.


  • Do you know any video/guide/code-example which demonstrates your point of 1-to-1 mapping? I would like to learn what you mean. Jul 27 '09 at 11:19

Here, you can see that Google leave the .py extension.

  • Thank you for pointing that out! -- It is still difficult to see that, since it it not at the end alone. Jul 26 '09 at 22:22

Not directly related to your question, but not only websites are created with Python. The entire AI engine for EVE online (a MMP space game) is written in Python I believe.



Google Search .py

9th Result http://schmidt.nuigalway.ie/cs103/python/graphics.py

Thats at least one website with a file that ends in .py - there are probably more.


This difference is mostly caused by the way the request is dispatched.

The websites which show a file extension are mostly dispatching directly via the filesystem. When you see a URL path like /example/index.php, the web server goes to the example directory under the document root, opens the index.php file, and executes its contents (simplifying a bit). Since they are going through the filesystem, they tend to use the whole filename, including the extension.

The websites which do not show a file extension are mostly dispatching in some other way. For instance, in Django, there is (simplifying a bit) a table mapping the URL path to the code which will process the request. Since here there is no relation between the URL path and the filesystem path, the mapping can be completely arbitrary, and there is no reason to add a "fake" extension.

There are exceptions to the first case; for instance, with mod_rewrite you can remap the URL path, easily hiding any file extension (this is very common).

The reason you do not see often the first case with Python is that most people use web frameworks when building sites with Python, which do their own dispatching. This is also common even with PHP (Drupal is a common PHP framework), but creating "standalone" pages (without a framwork) is more common with PHP than with Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.