simple command line ./manage.py shell puzzle

81 views
Skip to first unread message

cseb...@gmail.com

unread,
Apr 21, 2022, 1:06:45 PM4/21/22
to Django users
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

Larry Martell

unread,
Apr 21, 2022, 1:18:04 PM4/21/22
to django...@googlegroups.com
What do you expect to happen?

cseb...@gmail.com

unread,
Apr 21, 2022, 2:53:05 PM4/21/22
to Django users
The script should quietly exist without error.

I actually have a longer script that is the real workhorse.  
I just made this tiny one to showcase the issue.  

I can past  

import datetime ; [datetime for e in [1, 2]]

into a regular Python3 shell as well as the Django shell and all is fine.

The problem I think has something to do with how I inject
it into the Django shell via "< test".

cs

Larry Martell

unread,
Apr 21, 2022, 3:10:39 PM4/21/22
to django...@googlegroups.com
On Thu, Apr 21, 2022 at 2:53 PM cseb...@gmail.com <cseb...@gmail.com> wrote:
>
> The script should quietly exist without error.
>
> I actually have a longer script that is the real workhorse.
> I just made this tiny one to showcase the issue.
>
> I can past
>
> import datetime ; [datetime for e in [1, 2]]
>
> into a regular Python3 shell as well as the Django shell and all is fine.

$ python3
Python 3.8.10 (v3.8.10:3d8993a744, May 3 2021, 08:55:58)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime ; [datetime for e in [1, 2]]
[<module 'datetime' from
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/datetime.py'>,
<module 'datetime' from
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/datetime.py'>]

It does not do anything.
> --
> 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 django-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c4a36ffb-8c7a-477f-9d26-f6b898178d91n%40googlegroups.com.

cseb...@gmail.com

unread,
Apr 21, 2022, 3:28:37 PM4/21/22
to Django users
Yes I agree.  But why does it complain that datetime doesn't exist when I do "  ./manage.py shell < test " ?

Shouldn't it also not do anything like when I paste into a Python3 or Django shell as you did?

cs

Antonis Christofides

unread,
Apr 22, 2022, 3:00:17 AM4/22/22
to django...@googlegroups.com

It works fine here. I entered your commands exactly and the second one does not provide any output.

What shell do you have? And what version of Python and Django? And what OS?

cseb...@gmail.com

unread,
Apr 22, 2022, 2:47:08 PM4/22/22
to Django users
Antonis

Thanks.  Here are the answers to your questions...

OS: Ubuntu 20.04.3 LTS

Django version: 2:2.2.12-1ubuntu0.11

Django shell version: Python 3.8.10 

Chris

cseb...@gmail.com

unread,
Apr 22, 2022, 3:05:53 PM4/22/22
to Django users
After some more investigating.  All the issues seem to be because of the FOR LOOP.

For some strange reason, when I pipe in the Python code you can't have a for loop that way.

cs

On Friday, April 22, 2022 at 2:00:17 AM UTC-5 Antonis Christofides wrote:

Derek

unread,
Apr 25, 2022, 1:44:02 AM4/25/22
to Django users
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

Derek

unread,
Apr 25, 2022, 1:49:09 AM4/25/22
to Django users
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

Antonis Christofides

unread,
Apr 25, 2022, 8:23:00 AM4/25/22
to django...@googlegroups.com

The problem also occurs here when I use Django 2.2. It seems it's Django bug 32183, which in the meantime has been fixed.

https://code.djangoproject.com/ticket/32183

Reply all
Reply to author
Forward
0 new messages