› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Understanding from . import views in Django: How Python Knows the “Current Folder”
- This topic is empty.
-
AuthorPosts
-
April 23, 2026 at 5:03 am #6444
When beginners see this line in Django:
from . import views
it often raises a good question:
How does Python know where to look?
Does it mean the folder where you ran the project?
Does it mean the root folder likewiki?
Or the folder where the file itself is placed?Let’s make it simple.
Short Answer
The dot
.means:Import from the same package/folder where this file is located.
So if this code is inside:
encyclopedia/urls.py
Then:
from . import views
means:
import encyclopedia.views
Python looks beside
urls.pyfor a file named:encyclopedia/views.py
Example Django Structure
wiki/
│ manage.py
│
├── wiki/
│ settings.py
│ urls.py
│
└── encyclopedia/
init.py
urls.py
views.py
models.pyInside:
encyclopedia/urls.py
you may write:
from . import views
This tells Python:
➡️ Stay inside the
encyclopediapackage
➡️ Importviews.py
Important Clarification
It does not mean:
the folder where you launched
manage.pyyour desktop folder
the project root automatically
It means:
The package location of the file containing the import statement.
Why Django Uses This
Each app can manage itself.
Example:
encyclopedia/views.pyauctions/views.pynetwork/views.pySo each app’s
urls.pyimports its own views cleanly.
Memory Trick
If you are inside:
someapp/urls.py
Then:
from . import views
means:
👉 “Look next to me for
views.py”
Bonus: More Relative Imports
from . import views # same folder
from .. import settings # parent folder.= current package..= parent package
Final Takeaway
When Django uses:
from . import views
Python knows the location because it uses the folder/package of the file where that code is written.
So yes — the file’s own package becomes the reference point for the dot.
-
AuthorPosts
- You must be logged in to reply to this topic.
