0

I am a newbie to Python and Django. I have been playing with the Polls solution available in VS 2017 to help learn Django and Python.

My question has to do this execution of python script files. By strategically adding print statements within the .py files of this solution I am trying to understand the order in which the execution occurs.


The first file executed (that is listed on the Solutions Explorer) is manage.py.

From there execution jumps to settings.py but it passes through the __init__ file first (which is empty but I added a print statement anyways so I was able to track it to that point).

The settings.py file as expected sets variables, etc.. but the last file in the settings.py file is not a call, it is just an assignment of STATIC_ROOT, yet the execution jumps to the models.py via the _init_ once the last line of the settings.py is executed. The question is how and why ?

Also when the last line of a python script is reached what happens?

Is there an order to execution of various "Packages" or "Modules"?

Are script files linked using import statements?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    `settings` does not call `models`; it is the other way around. Since `models.py` (or some other module) imports `settings.py` (or more correctly `django.conf.settings`) your `print` statements are executed. See [this question](https://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) for details. – Selcuk Jul 20 '18 at 03:01
  • This is not php batch task script mode,this is web server runtime.You can step into it to get the order of init process ,but is no value. You should to understand MVT pattern to get the runtime structure。 – Hayden Jul 20 '18 at 06:57
  • Given that I am "running" the solution from inside VS 2017 (hit the Web Server green arrow button) the print statements placed on the top of each .py file clearly indicate a sequential pattern. Your statements are helpful, although the print statement indicate that settings.py gets executed before models.py (But Selcuk is no doubt correct) which leads to Hayden's comment "but is no value" these two comments indicate that what the print statements are stating is irrelevant or inaccurate? I think I need to investigate the MVT as Selcuk suggested. – alphamoose Jul 20 '18 at 12:20
  • After further reading am I correct to assume that all I am seeing with my print statements in the order in which python processes the script file (*.py files) to initialize my solution. Although the files are being executed, the order is not relevant? That does not make sense either. The order would matter if I were setting up variables that were dependent on other variables? More reading is required. – alphamoose Jul 20 '18 at 17:15

0 Answers0