cheesus
cheesus

Reputation: 1181

strptime throws error when run in docker container

When I run

from datetime import datetime
print(datetime.strptime('2020-01-15 09:20:00.00+00:00', '%Y-%m-%d %H:%M:%S.%f%z'))

It prints out

2020-01-15 09:20:00+00:00

When I run a docker container with those exact same lines, it throws this error:

File "/vdp/base_functions.py", line 9, in <module>
j2lm@VB    |     print(datetime.strptime('2020-01-15 09:20:00.00+00:00', '%Y-%m-%d %H:%M:%S.%f%z'))
j2lm@VB    |   File "/usr/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
j2lm@VB    |     tt, fraction = _strptime(data_string, format)
j2lm@VB    |   File "/usr/lib/python3.6/_strptime.py", line 362, in _strptime
j2lm@VB    |     (data_string, format))
j2lm@VB    | ValueError: time data '2020-01-15 09:20:00.00+00:00' does not match format '%Y-%m-%d %H:%M:%S.%f%z'

How can that be?

Upvotes: 2

Views: 341

Answers (1)

Blackpointfin
Blackpointfin

Reputation: 134

How can that be?

I'm assuming you're developing snippets in some IDE, or Jupyter and than pasting it into the code that is run within a container. If so, make sure to use the same venv in your container image, that you are using to develop locally, to avoid issues like these, that can be tedious to debug.

If I use python 3.7 I cannot reproduce the error, so you might solve the error just by using an up-to-date docker image and run the script with a more recent python version (Yours uses 3.6).

Here is a sophisticated guide on how to set up your Docker file in a way that you have full control over which version of python is used, that I found very helpful.

Upvotes: 1

Related Questions