0

My python script run fine when ran manually but whenever run from cron job it throws UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in position 0: ordinal not in range(128)

myfile.py:

print(u"\u2122")

I am on macos high sierra and python version is 3.6.0

ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
  • Can you paste your code?, that will help people know what is causing the issue. – shuberman Aug 19 '19 at 04:35
  • @mishsx pasted. code is simple print – ishandutta2007 Aug 19 '19 at 06:02
  • Is it supposed to print `™` ? – shuberman Aug 19 '19 at 06:08
  • @mishsx yes. typing in terminal `python myfile.py` prints that. – ishandutta2007 Aug 19 '19 at 06:11
  • But when you run the same thing via job it gives you an error? Looks like the problem is not on the code side but maybe the way you run the job. Please provide details of what job is it and how it is run? – shuberman Aug 19 '19 at 06:13
  • It was working fine until few days. Some change in environment variable might have screwed it up. Tried [this](https://stackoverflow.com/a/54448940/865220) didn't help. – ishandutta2007 Aug 20 '19 at 04:06
  • In that case we can be sure that this is not a python error but something on the job side that is screwing up. I suggest you to collect all the details about the job and see if there are changes in variables done by some DEV that could be causing this. – shuberman Aug 20 '19 at 04:09
  • If you follow a TDD approach at your company, you should have different environments like DEV, QA, PROD. In that case try to compare with those enviroment and check what are the changes. Is it not working in PROD or all the environments ? If not working in one of them then compare to check diferences, that will near you to the clue. – shuberman Aug 20 '19 at 04:11
  • 1
    Just made it work by putting `LC_ALL="en_US.UTF-8" ` on top of my crontab list. This is kind of hack, in other environment it works without this. – ishandutta2007 Aug 20 '19 at 04:14
  • 1
    It struck me as earlier I had fixed `PATH` variable issues like this as well. Ideally in crontab file there should be nothing but jobs. So something at OS level must had to be rebuilt or reinstalled to make the fix proper – ishandutta2007 Aug 20 '19 at 04:16
  • 1
    you should research on why it works in other environments but only this. Is there some setting that would be causing this? Sooner or later someone might face the same issue tomorrow, so make sure to put comments that would help the person who sees this next time. – shuberman Aug 20 '19 at 04:22

1 Answers1

1

Fixed it by putting on top of crontab:

LC_ALL="en_US.UTF-8"

ishandutta2007
  • 16,676
  • 16
  • 93
  • 129