wxPython4.1 / windows / python3.7 : strptime / locale issue

Hi all,

test_strptime.py (1.2 KB)

the attached code runs fine on :

  • various linux, with wxpython 4.0.7 / 4.1.0, and python 3.6 / 3.7 / 3.8
  • windows, with wxpython 4.0.7, and python 3.7

but it failes in that case :

  • windows, with wxpython 4.1.0 and python 3.7
    I get this following error :
---> locale.GetName() = fr_FR
Traceback (most recent call last):
  File "test_strptime.py", line 39, in OnInit
    self.diag = Test()
  File "test_strptime.py", line 32, in __init__
    date_out = date2num(datetime.datetime.strptime(date_in, '%Y%m%d%H%M'))
  File "C:\Users\regis\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 277, in <module>
    _TimeRE_cache = TimeRE()
  File "C:\Users\regis\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 191, in __init__
    self.locale_time = LocaleTime()
  File "C:\Users\regis\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 69, in __init__
    self.lang = _getlang()
  File "C:\Users\regis\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 28, in _getlang
    return locale.getlocale(locale.LC_TIME)
  File "C:\Users\regis\AppData\Local\Programs\Python\Python37\lib\locale.py", line 587, in getlocale
    return _parse_localename(localename)
  File "C:\Users\regis\AppData\Local\Programs\Python\Python37\lib\locale.py", line 495, in _parse_localename
    raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: fr-FR

Can you reproduce this kind of issue too with windows system, wxpython 4.1.0 only, and python3.7 ?
What can be done to fix this ?

Note :
I use virtualenv in case this is relevant

Thank you for your attention

Following…I have the same issue on a wxPython 4.1.0 Python 3.8 program running on Win10. Hope we find a workaround soon. I kind of need strptime to work!

See my reply in this topic: What is wxPython doing to the locale to makes pandas crash?

In the other thread that Robin linked, you’ll see my solution (a bit harsh) and Karsten’s response that makes it a little cleaner and more international friendly. Basically, I hard set the locale to “en_US” right after I instantiated the wx app and all of my strptime functions started working just fine. Karsten’s more elegant approach is to first query the locale then replace the “-” with “_” so that everyone is synchronized. Like this:

import locale
hyphenated_locale = wx.WhateverGivesTheStrangeLocale()
locale.setlocale(locale.LC_ALL, hyphenated_locale.replace('-', '_'))

In our case, maybe the “WhatverGivesTheStrangeLocale” function would be “locale.getlocale(locale.LC_TIME)” based on your error messages above. I can’t test right now, but that should get you in the right direction. I would start by hard setting to “fr_FR” right after you instantiate the GUI to make sure it works, then start figuring out how to do this in a cleaner way.

Thanks a lot Suart for your answer.

I tried your solution inspired by Karsten, but I had and error :

Traceback (most recent call last):
  File "test_strptime.py", line 69, in OnInit
    self.diag = Test()
  File "test_strptime.py", line 22, in __init__
    locale.setlocale(locale.LC_ALL, hyphenated_locale.replace('-', '_'))
AttributeError: 'tuple' object has no attribute 'replace'
OnInit returned false, exiting...

So I changed my mind, and I just tried to put right after the first instantiation of my main class this unique line :

locale.setlocale(locale.LC_ALL, '' )

And all seems working fine in my real software I’m working on.
I just have to check on my others Windows systems in different languages, to check if it still working on them too.

But in the sample I send in my first post, this one line workaround didn’t worked as expected, I had to add a Robin’s tip in my main class :

    def InitLocale(self):

I join the complete code fixing my example inserted in my first post.

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import wx
import locale

import datetime
from matplotlib.dates import date2num

import locale

class Test (wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1,
                          'Test', size=(550, 350))   
        date = wx.TextCtrl(self, wx.ID_ANY,"", style=wx.TE_READONLY )

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(date, 0, wx.ALL, 5)
        date_in = "202005260800"
        date_out = date2num(datetime.datetime.strptime(date_in, '%Y%m%d%H%M'))

        print ("===> date_out = " + str (date_out) )

class MyApp(wx.App):
    def OnInit(self):
        self.diag = Test()
        locale.setlocale(locale.LC_ALL, '' )
        return True

    def InitLocale(self):
if __name__ == "__main__":
    app = MyApp(0)    

Thank you Stuart, Karsten and Robin, I wish you the best.

Note :
I will post again if I notice any issue on my other language windows systems.

1 Like