Reputation: 31
I am using Django with Python and I don't understand this:
from . import views
I would like to know what I am importing when I type that.
Upvotes: 1
Views: 1968
Reputation: 546
import from same directory , ".." mean import from upper directory
Upvotes: 1
Reputation: 870
You can import files, modules and packages using relative or absolute paths.
Take a look at this project:
-- project_folder
--project_name
──settings.py
──init.py
──urls.py
──wsgi.py
--app1
──__init__.py
── models.py
── views.py
── admin.py
-- package1_folder
── hello_world.py
A relative import is used to retrieve a resource relative to the current path you are on.
So if you are currently working inside app1 -> views.py
and you want to import hello_world.py
to your views you can use .
to specify a relative import to the current file you are working on.
So to import hello_world.py
we could use from .package1_folder import hello_world
.
If you just specify from . import models
you are importing the models.py resource from the current folder you are on(app1).
An absolute import on the other hand is used to import a resource from anywhere in the project using the full path.
For example, you can use from app1.package1_folder import hello_world
anywhere in your project and you will successfully import the file.
Upvotes: 2
Reputation: 140
You import views.py from the location of the python script that calls import statment.
Upvotes: 1