CaptainDriftwood
CaptainDriftwood

Reputation: 5728

Error in manage.py script when running cron job for django inside docker container

I have a docker container runnning a django application that is also running a cron job inside it. The managed command keeps failing due to this error:

Line 14 in manage.py

line 14
) from exc
     ^
SyntaxError: invalid syntax

My cron job looks like this:

0 1 * * 1-5 python manage.py myCommand

I tried changing it to this to see if that would do the trick:

0 1 * * 1-5 /usr/local/bin/python /absolute/path/to/project/manage.py myCommand

Upvotes: 1

Views: 519

Answers (1)

arudzinska
arudzinska

Reputation: 3329

This is a typical error you get when using a wrong Python version. Following your comment under the question:

The docker container is using python 3.5. When I run python -V, I get Python 3.5.6

You can see that your user's $PATH points to Python 3.5.6 when using the shell session you open. There is one thing about cron jobs that often leaves people stuck with problems: cron opens a different shell and doesn't have access to your $PATH. So in your case it points to Python 2.x.

There are many ways to overcome this. Probably the easiest one is to check where your Python 3 is located by running

$ which python3

and using the path from the output in your cron command. For example, for my system it would be:

0 1 * * 1-5 /usr/bin/python3 manage.py myCommand

Upvotes: 4

Related Questions