I thought I'd start python, well, after all
print("Hello world")
Yeah, good.
print ("Japanese")
Traceback (most recent call last):
File "/tmp/a.py", line 1, in <module>
print("\u65e5\u672c\u8a9e");
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
:cry: :-1:
After a lot of research, I found that I had to convert between unicode and byte.
print ("Japanese" .encode ("utf-8"))
b'\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e'
No, that's not the case.
setenv LANG ja_JP.UTF-8
Japanese
Well, it worked. But there are more problems!
When you start cron, the parent process is cron, so naturally it inherits the cron environment. That is the story of what kind of locale is set for the entire system. However, since the policy of the entire system is unified with basic C (when ls or date is set to the Japanese locale format, various things do not work), the cron environment is naturally LANG = C. It can't be changed.
To start the script, write #! / usr / local / bin / python3
on the first line. There are many ways to call this, but it seems to be shebang. So, the one called by this cannot use shebang twice. In other words, when #! / usr / local / bin / foo
, the first line of / usr / local / bin / foo should not be #! / usr / local / bin / bar
. .. I did not know that. Perhaps you are calling an exec () series of functions in a system call.
The idea of making / usr / local / bin / python3 a shell script was frustrated, so I wrote it in C.
python3.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv){
putenv("LANG=ja_JP.UTF-8");
argv[0] = "/usr/local/bin/python3.5";
argv[argc] = NULL;
execv("/usr/local/bin/python3.5",argv);
}
Now it works only with the first print ("Japanese ")
. (I don't need encode anymore)
python3 is no longer a symbolic link, but it's a workaround, so please be patient.
Somehow solved. The road to Pythonista is steep
Recommended Posts