Actually, there does seem to be some further issue: echo "import datetime; [datetime.date(2022,4,e) for e in [1,2]]" > test.py
Also triggers an error when used in the Django shell, but works when run as a normal Python script: python test.py On Monday, 25 April 2022 at 07:44:02 UTC+2 Derek wrote: > It fails because the second datetime in your test script is taken to be a > variable and it is not one you have already defined; hence the error: > "NameError: name 'datetime' is not defined". > > Try this instead: > > echo "import datetime ; [e*e for e in [1,2]]" > test > > On Thursday, 21 April 2022 at 19:06:45 UTC+2 [email protected] wrote: > >> Why does this fail?... >> >> % echo "import datetime ; [datetime for e in [1, 2]]" > test >> >> % ./manage.py shell < test >> >> Traceback (most recent call last): >> File "./manage.py", line 8, in <module> >> django.core.management.execute_from_command_line(sys.argv) >> File >> "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line >> 381, in execute_from_command_line >> utility.execute() >> File >> "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line >> 375, in execute >> self.fetch_command(subcommand).run_from_argv(self.argv) >> File "/usr/lib/python3/dist-packages/django/core/management/base.py", >> line 323, in run_from_argv >> self.execute(*args, **cmd_options) >> File "/usr/lib/python3/dist-packages/django/core/management/base.py", >> line 364, in execute >> output = self.handle(*args, **options) >> File >> "/usr/lib/python3/dist-packages/django/core/management/commands/shell.py", >> line 92, in handle >> exec(sys.stdin.read()) >> File "<string>", line 1, in <module> >> File "<string>", line 1, in <listcomp> >> NameError: name 'datetime' is not defined >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bf2e86b8-6c2a-4070-a53e-3b1f8e538039n%40googlegroups.com.

