› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Why Python Imports views Instead of views.py in Django
- This topic is empty.
-
AuthorPosts
-
March 15, 2026 at 6:06 am #6203
When beginners start learning Django, they often come across the following code in a
urls.pyfile:from django.urls import path from . import views urlpatterns = [ path("", views.index, name="index") ]A common question that arises is:
Why does the code say
viewsinstead ofviews.py?Let’s break this down in a simple and clear way.
Python Imports Use Module Names, Not File Extensions
In Python, when we import code from another file, we do not include the
.pyextension.For example, if we have a file:
views.pyPython imports it using:
import viewsor
from . import viewsPython automatically understands that
viewsrefers to the fileviews.py.This is part of Python’s module system.
What Is a Module in Python?
A module is simply a Python file that contains code such as functions, classes, or variables.
Example file:
views.pyExample content:
def index(request): return "Hello, world!"Here:
views.py→ the fileviews→ the module name used for importing
So when Python imports the file, it refers to it by the module name (
views) rather than the file name (views.py).
Why Python Does Not Use
.pyin ImportsPython was designed so that imports look clean and work across different types of modules.
Instead of writing:
import views.py # ❌ invalidPython expects:
import views # ✅ correctWhen Python sees
import views, it automatically searches for:views.pyviewspackage directory- compiled Python modules
This flexibility makes the import system more powerful.
What
from . import viewsMeans in DjangoIn Django apps, you will often see:
from . import viewsThe dot (
.) means:Import the module from the current directory (current app).
So Python looks for:
current_app_folder/views.pyand loads it as a module named
views.
How This Connects to URL Routing
Consider this line in Django:
path("", views.index, name="index")Here:
views→ the module (views.py)index→ a function inside that file
Example
views.py:from django.http import HttpResponse def index(request): return HttpResponse("Hello, world!")Request flow becomes:
User visits website ↓ Django checks urls.py ↓ path("", views.index) ↓ index() function runs ↓ Response sent to browser
Key Rule to Remember
When importing Python files:
File Name Import Statement views.pyimport viewsmodels.pyimport modelsutils.pyimport utilsAlways use the module name, not the
.pyextension.
Final Takeaway
When you see this in Django:
from . import viewsit simply means:
Import the
views.pyfile located in the current Django app so that its functions (likeindex) can be used in URL routing.Understanding this small concept helps clarify how Django connects URLs, views, and responses, which is a core part of how web applications work.
-
AuthorPosts
- You must be logged in to reply to this topic.

