When I try to import pandas into my wxPython application a "ValueError: unknown locale: en-US" exception is thrown. I can avoid this by importing pandas before the application starts, but that seems like a silly requirement.
This is easily repeatable in a simple wx app:
import wx
class TestApp(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
import pandas
self.frame = wx.Frame(None, size=wx.Size(670,670), title='Test App')
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.frame.SetSizer(self.sizer)
if __name__ == '__main__':
app = TestApp()
app.MainLoop()
When I run this app from my virtual environment I get this stack trace:
(test) PS env_test> python .\test2.py Traceback (most recent call last):
File ".\test2.py", line 16, in <module>
app = TestApp()
File ".\test2.py", line 8, in __init__
import pandas
File "E:\Envs\test\lib\site-packages\pandas\__init__.py", line 32, in <module>
from pandas._libs import hashtable as _hashtable, lib as _lib, tslib as _tslib
File "E:\Envs\test\lib\site-packages\pandas\_libs\__init__.py", line 3, in <module>
from .tslibs import (
File "E:\Envs\test\lib\site-packages\pandas\_libs\tslibs\__init__.py", line 3, in <module>
from .conversion import localize_pydatetime, normalize_date
File "pandas\_libs\tslibs\c_timestamp.pxd", line 7, in init pandas._libs.tslibs.conversion
File "pandas\_libs\tslibs\c_timestamp.pyx", line 1, in init pandas._libs.tslibs.c_timestamp
File "pandas\_libs\tslibs\tzconversion.pyx", line 1, in init pandas._libs.tslibs.tzconversion
File "pandas\_libs\tslibs\timedeltas.pyx", line 1, in init pandas._libs.tslibs.timedeltas
File "pandas\_libs\tslibs\offsets.pyx", line 1, in init pandas._libs.tslibs.offsets
File "pandas\_libs\tslibs\ccalendar.pyx", line 13, in init pandas._libs.tslibs.ccalendar
File "pandas\_libs\tslibs\strptime.pyx", line 625, in init pandas._libs.tslibs.strptime
File "pandas\_libs\tslibs\strptime.pyx", line 530, in pandas._libs.tslibs.strptime.TimeRE.__init__
File "pandas\_libs\tslibs\strptime.pyx", line 405, in pandas._libs.tslibs.strptime.LocaleTime.__init__
File "pandas\_libs\tslibs\strptime.pyx", line 362, in pandas._libs.tslibs.strptime._getlang
File "c:\python37\lib\locale.py", line 587, in getlocale
return _parse_localename(localename)
File "c:\python37\lib\locale.py", line 495, in _parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: en-US
I'm running a virtual environment on Windows 10 using python3.7.4 with minimal packages installed:
Package Version
--------------- -------
numpy 1.18.4
pandas 1.0.3
Pillow 7.1.2
pip 20.1
python-dateutil 2.8.1
pytz 2020.1
setuptools 46.1.3
six 1.14.0
wheel 0.34.2
wxPython 4.1.0
I can import pandas outside of the app just fine:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> df = pandas.DataFrame()
>>>
I'm flummoxed, and I've been unable to find answers elsewhere. Other pandas "unknown locale" errors seem to be related to not having the locale exported on OSX.
Set your locale back to the default setting with setlocale right after you initialize the wx.App instance. In most environments, this seems to fix the locale problems caused by wxpython in the rest of your code, and you can continue to use wxpython 4.1.
If you continue to have trouble, see this discussion about subclassing wx.App
app = wx.App(False)
locale.setlocale(locale.LC_ALL, '')