This weekend I encountered a weird problem during programming C extension modules for Python. For some obscure reason floats from my C extension modules were formatted with a comma as separater (e.g. 123,456
) instead of with the more familiar point (e.g. 123.456
). Obviously some locale related problem. Most of my desktop and applications are set up for Dutch (my native language), but when I'm programming/working I use English and scientific conventions (e.g. a point as decimal separator). After isolating the problem I found out it was related to importing the pylab (aka Matplotlib) module (which I started using for plotting graphs and figures from Python). The following situation illustrates the problem.
The very minimal but operational C extension module pylabproblem.c
:
#include <Python.h>
#include <stdio.h>
static PyObject *
hello(PyObject *self, PyObject *args)
{
printf("Hello there, check out this float: %.3f\n", 123.456);
return Py_BuildValue("");
}
static PyMethodDef Methods[] = {
{"hello", hello, METH_VARARGS, "Say hello and show a float."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initpylabproblem(void)
{
(void) Py_InitModule("pylabproblem", Methods);
}
which I compiled (under Kubuntu Linux) to a shared library pylabproblem.os
as follows:
gcc -fPIC -I/usr/include/python2.4 -c -o pylabproblem.os pylabproblem.c
gcc -shared -o pylabproblem.so pylabproblem.os
Then I used this freshly made C extension module in the following simple Python script pylabproblen-test.py
:
#!/usr/bin/env python
import pylabproblem
pylabproblem.hello()
import pylab
pylabproblem.hello()
This script first imports the C extension module pylabproblem
and calls its hello()
function. Then it imports the pylab/matplotlib module and calls pylabproblem
's hello()
function again. This is the output from the script when I ran it:
$> ./pylabproblem-test.py
Hello there, check out this float: 123.456
Hello there, check out this float: 123,456
Not very practical if you want to postprocess that output in a further stage (with grep, gnuplot, ...). Apparently importing pylab
causes some changes of locale settings in this situation.
I checked my locale settings and found that not very much was setted: only LANG="nl_BE.UTF-8"
and LANGUAGE="nl_BE:nl:en_GB:en"
. After I explicitly set LC_NUMERIC
to for example "C"
, I got what I wanted:
$> export LC_NUMERIC="C"
$> ./pylabproblem-test.py
Hello there, check out this float: 123.456
Hello there, check out this float: 123.456
I think I better add a line export LC_NUMERIC="C"
to my ~/.bashrc
.