I'm just started to learning backend web development using Python from a tutorial [here][1].
The first project is a simple Hello World respond to requests for website home page.
Whole the program for above project is consist of 3 files:
myWebsite/
app/
__init__.py
views.py
run.py
And the Python files are as below :
__init__.py
from flask import Flask
app = Flask (__name__)
from app import views
views.py
from app import app
@app.route ('/')
def sayHello():
return "Hello World"
run.py
from app import app
app.run (debug = True)
My origin question:
The program will execute by interpreting run.py
. As far as I know in the first line of this file, from app import app
, it search __init__.py
for an object named app
and if it found, make a it available for the next lines, right?
Well, that seems OK for me (I think!).
The question is, why we need to from app import app
in views.py
while I don't want to execute it! I mean I just want to use this file in my __init__.py
file, so I think that enough to import it in __init__.py
and I don't need to from app import app
in views.py
. Am I wrong?
As a side question :
When I write from x import y
, what is x
and what is y
?
- Is
x
a directory or a file? - Is
y
a file or an object in a file named__init__.py
?
When I write import z
, what is z
? a file or an object inside one of the libraries?
I know that my question seems simple for experts, but I migrate from C to Python, and look at import
like #include in C and I think this is origin of my problem. I read some Q&A in SO related to my issue, but I am confused about it yet.