<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">Ciao a tutti, un piccolo rompicapo.</div><div class="">quando eseguo il comando shell di Django, ho un comportamento inaspettato dell' interprete python che proprio non riesco a spiegare:</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Questo é il semplice snippet di codice che voglio eseguire:</div><div class="">Quando tento l’escussione con <span style="color: rgb(235, 235, 235); font-size: 9pt; background-color: rgb(43, 43, 43);" class="">python manage.py shell < test.py </span> ottengo un errore e non riesco a capire a che modulo python vengono associate le funzioni definite.</div><div class=""><br class=""></div><div class="">File test.py</div><div class=""><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Menlo';font-size:9.0pt;" class=""><pre style="font-family: Menlo; font-size: 9pt;" class=""><span style="color:#cc7832;" class="">def </span>foo():<br class=""> print(<span style="color:#6a8759;" class="">'foo() --start--'</span>)<br class=""> print(<span style="color:#6a8759;" class="">'foo() --stop--'</span>)<br class=""><br class=""><span style="color:#cc7832;" class="">def </span>bar():<br class=""> print(<span style="color:#6a8759;" class="">'bar() --start--'</span>)<br class=""> print(<span style="color:#6a8759;" class="">'bar() --stop--'</span>)<br class=""><br class=""><span style="color:#cc7832;" class="">def </span>main():<br class=""> print(<span style="color:#6a8759;" class="">'main() ### start ###'</span>)<br class=""> foo()<br class=""> bar()<br class=""> print(<span style="color:#6a8759;" class="">'main() ### stop ###'</span>)<br class=""><br class="">foo()<br class="">bar()<br class="">main()</pre></pre></div><div class=""><b class="">Risultato aspettato:</b><span class="Apple-tab-span" style="white-space:pre"> </span></div><div class=""><pre style="font-size: 9pt; background-color: rgb(43, 43, 43);" class=""><pre style="font-size: 9pt;" class=""><div style="caret-color: rgb(0, 0, 0); white-space: normal;" class=""><div class=""><font color="#ebebeb" face="Menlo" class="">foo() --start--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">foo() --stop--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">bar() --start--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">bar() --stop--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">main() ### start ###</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">foo() --start--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">foo() --stop--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">bar() --start--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">bar() --stop--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">main() ### stop ###</font></div></div></pre></pre></div><div class=""><div class=""><br class=""></div></div><div class=""><span style="background-color: rgb(0, 0, 0);" class=""><font color="#ffffff" class=""><br class=""></font></span></div><div class=""><div class=""><b class=""><font color="#ff2600" class="">Se provo a eseguirlo passando il file come stdinput alla shell di Django, qualcosa nella gestione dei moduli non funziona e il codice non gira:</font></b></div></div><div class=""><div class=""><div class=""><pre style="font-size: 9pt; background-color: rgb(43, 43, 43);" class=""><pre style="font-size: 9pt;" class=""><div style="white-space: normal;" class=""><span style="font-family: Helvetica;" class=""><font color="#ebebeb" class="">python manage.py shell < test.py</font></span></div></pre></pre></div><div class=""></div></div></div><div class=""><div class=""><pre style="font-size: 9pt; background-color: rgb(43, 43, 43);" class=""><pre style="font-size: 9pt;" class=""><div style="white-space: normal;" class=""><div class=""><font color="#ebebeb" face="Menlo" class="">foo() --start--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">foo() --stop--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">bar() --start--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">bar() --stop--</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">main() ### start ###</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">Traceback (most recent call last):</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> File "./manage.py", line 21, in <module></font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> main()</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> File "./manage.py", line 17, in main</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> execute_from_command_line(sys.argv)</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> File "/private/tmp/test/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> utility.execute()</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> File "/private/tmp/test/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> self.fetch_command(subcommand).run_from_argv(self.argv)</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> File "/private/tmp/test/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> self.execute(*args, **cmd_options)</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> File "/private/tmp/test/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> output = self.handle(*args, **options)</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> File "/private/tmp/test/lib/python3.7/site-packages/django/core/management/commands/shell.py", line 95, in handle</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> exec(source)</font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> File "<string>", line 17, in <module></font></div><div class=""><font color="#ebebeb" face="Menlo" class=""> File "<string>", line 11, in main</font></div><div class=""><font color="#ebebeb" face="Menlo" class="">NameError: name 'foo' is not defined</font></div></div></pre></pre></div><div class=""></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Se lo eseguo passandolo com stdinput direttamente a python funziona tutto, le nuove funzioni vengono inserite nel modulo __main__ <module '__main__' (built-in)></div><div class=""><div class=""><pre style="font-size: 9pt; background-color: rgb(43, 43, 43);" class=""><pre style="font-size: 9pt;" class=""><div style="white-space: normal;" class=""><span style="font-family: Helvetica;" class=""><font color="#ebebeb" class="">python < test.py</font></span></div></pre></pre></div><div class=""></div></div><div class="">Il codice del comando shell di Django, é molto semplice:</div><div class="">dopo aver fatto il bootstrap del progetto Django, non fa altro che </div><div class=""><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Menlo';font-size:9.0pt;" class="">exec(sys.stdin.read())</pre><div class=""><br class=""></div><div class="">Provando a mano, </div><div class="">Se lancio la shell di Django in modalitá interattiva</div></div><div class=""><pre style="font-size: 9pt; background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: Menlo;" class="">python manage.py shell</pre></div><div class="">Metto i sorgenti in una stringa e la eseguo con exec() un po come fa il comando shell, <b class="">funziona tutto</b></div><div class=""><div class=""><pre style="font-size: 9pt; background-color: rgb(43, 43, 43);" class=""><pre style="font-size: 9pt;" class=""><div style="white-space: normal;" class=""><div class=""><font color="#ebebeb" class="">Python 3.7.4 (default, Sep 7 2019, 18:27:02)</font></div><div class=""><font color="#ebebeb" class="">[Clang 10.0.1 (clang-1001.0.46.4)] on darwin</font></div><div class=""><font color="#ebebeb" class="">Type "help", "copyright", "credits" or "license" for more information.</font></div><div class=""><font color="#ebebeb" class="">(InteractiveConsole)</font></div><div class=""><font color="#ebebeb" class="">>>> source_code = '''</font></div><div class=""><font color="#ebebeb" class="">... def foo():</font></div><div class=""><font color="#ebebeb" class="">... print('foo() --start--')</font></div><div class=""><font color="#ebebeb" class="">... print('foo() --stop--')</font></div><div class=""><font color="#ebebeb" class="">...</font></div><div class=""><font color="#ebebeb" class="">... def bar():</font></div><div class=""><font color="#ebebeb" class="">... print('bar() --start--')</font></div><div class=""><font color="#ebebeb" class="">... print('bar() --stop--')</font></div><div class=""><font color="#ebebeb" class="">...</font></div><div class=""><font color="#ebebeb" class="">... def main():</font></div><div class=""><font color="#ebebeb" class="">... print('main() ### start ###')</font></div><div class=""><font color="#ebebeb" class="">... foo()</font></div><div class=""><font color="#ebebeb" class="">... bar()</font></div><div class=""><font color="#ebebeb" class="">... print('main() ### stop ###')</font></div><div class=""><font color="#ebebeb" class="">...</font></div><div class=""><font color="#ebebeb" class="">... foo()</font></div><div class=""><font color="#ebebeb" class="">... bar()</font></div><div class=""><font color="#ebebeb" class="">... main()</font></div><div class=""><font color="#ebebeb" class="">... '''</font></div><div class=""><font color="#ebebeb" class="">>>></font></div><div class=""><font color="#ebebeb" class="">>>> exec(source_code)</font></div><div class=""><font color="#ebebeb" class="">foo() --start--</font></div><div class=""><font color="#ebebeb" class="">foo() --stop--</font></div><div class=""><font color="#ebebeb" class="">bar() --start--</font></div><div class=""><font color="#ebebeb" class="">bar() --stop--</font></div><div class=""><font color="#ebebeb" class="">main() ### start ###</font></div><div class=""><font color="#ebebeb" class="">foo() --start--</font></div><div class=""><font color="#ebebeb" class="">foo() --stop--</font></div><div class=""><font color="#ebebeb" class="">bar() --start--</font></div><div class=""><font color="#ebebeb" class="">bar() --stop--</font></div><div class=""><font color="#ebebeb" class="">main() ### stop ###</font></div><div class=""><font color="#ebebeb" class="">>>></font></div></div></pre></pre></div><div class=""></div></div><div class=""><div class=""><br class=""></div></div><div class="">Cosa succede quando passo nel comando shell di Django?</div><div class="">Ciao a tutti.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>