-1

I am trying to run some cronjobs in django.

I have three of them, 2 of them are running flawlessly. but the third one is giving me the error:

../../monthly_abo_live.py", line 1
SyntaxError: encoding problem: with BOM

the first 2 lines of this file are:

1. # -*- coding: utf-8 -*-
2. from django.core.management.base import BaseCommand, CommandError
3. ...

the other 2 cronjobs are the same as this one. i am stuck - why only this one is complaining? It seems python doesnot support utf-8? it cannot be, right?

Signal15
  • 538
  • 5
  • 16
doniyor
  • 31,751
  • 50
  • 146
  • 233
  • 1
    UTF-8 is supported just fine. You have a bad BOM at the start of the file. How did you create it? – Wooble Sep 16 '14 at 18:43
  • @Wooble you mean how i created the file? just opened a new file and copied the code from daily_abo_live.py and adjusted. i use pycharm and it has utf-8 as encoding. might I have missed something here? – doniyor Sep 16 '14 at 18:45
  • 1
    Look at the file in a hex editor (or just in Python: `f = open('monthly_abo_live.py', 'rb'); start = f.read(80); print(repr(f))`) and that will show you what's wrong with the file (or, if you don't get it, post it here and someone else can explain it to you). Then you can figure out why that ended up in your file. – abarnert Sep 16 '14 at 18:59
  • 1
    The BOM is 2 to 5 bytes of binary data at the front of some files to let you know how its encoded (utf8/utf16/..., big/little endian). See [Byte order Mark](http://en.wikipedia.org/wiki/Byte_order_mark). – tdelaney Sep 16 '14 at 19:14

1 Answers1

-1

According to this page:

http://legacy.python.org/dev/peps/pep-0263/

you have two options...

To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:

    # coding=<encoding name>

or (using formats recognized by popular editors)

    #!/usr/bin/python
    # -*- coding: <encoding name> -*-

(Note: While the above is a direct quote, @tdelaney pointed out - and I agree - that instead of the fixed path #!/usr/bin/python one should use #!/usr/bin/env python.)

It looks like you're using part of the second option but did not include the required first line ( #!/usr/bin/python ). Try inserting that before your "coding" line and see what happens.

aldo
  • 2,687
  • 19
  • 34
  • All of the formats should work on either the first or second line. The shebang isn't (or shouldn't be...) required. – Wooble Sep 16 '14 at 18:48
  • should the path ``!/usr/bin/python`` show to correct path? i am using virtualenv, so the path is ``my_virtualenv/lib/python``, then? – doniyor Sep 16 '14 at 18:51
  • 1
    path should be `#!/usr/bin/env python` if you are on a linuxy machine, and is not needed on Windows. – tdelaney Sep 16 '14 at 19:17
  • Thank you for your time. but it was a dumb typo of mine :(. i wrote ``utf-9`` instead of ``utf-8``. – doniyor Sep 16 '14 at 20:12