Notebooks downloaded as Python files require a header stating the encoding #1156

Closed
Midnighter opened this Issue Dec 15, 2011 · 6 comments

Comments

Projects
None yet
4 participants
@Midnighter

The following problem occurred: I have a notebook with a mardown cell that contains non-ASCII characters. When I try to run the file I get the following error:

SyntaxError: Non-ASCII character '\xc3' in file bla.py on line 11, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

even though the offending character is commented.

Putting something like # -*- coding: utf-8 -*- in the first or second line of the file solves the problem, I think this should be standard procedure for exporting the notebook.

@takluyver

This comment has been minimized.

Show comment
Hide comment
@takluyver

takluyver Dec 15, 2011

Member

Yes, we already add this when using the %save magic, we should do it for the notebook as well.

Member

takluyver commented Dec 15, 2011

Yes, we already add this when using the %save magic, we should do it for the notebook as well.

@fperez

This comment has been minimized.

Show comment
Hide comment
@fperez

fperez Dec 15, 2011

Member

Actually I'd say this is a blocker on 0.12, b/c this simple line missing will make it much harder to use the nb. Plus, it's really easy to fix :)

Member

fperez commented Dec 15, 2011

Actually I'd say this is a blocker on 0.12, b/c this simple line missing will make it much harder to use the nb. Plus, it's really easy to fix :)

@bfroehle

This comment has been minimized.

Show comment
Hide comment
@bfroehle

bfroehle Dec 15, 2011

Contributor

This is also going to require some additional code on the read side as Python's implementation of JSON doesn't support comments as far as I can tell.

In [7]: import json

In [8]: print A
# -*- coding: utf-8 -*-
{"testing": "comments"}

In [9]: json.loads(A)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Contributor

bfroehle commented Dec 15, 2011

This is also going to require some additional code on the read side as Python's implementation of JSON doesn't support comments as far as I can tell.

In [7]: import json

In [8]: print A
# -*- coding: utf-8 -*-
{"testing": "comments"}

In [9]: json.loads(A)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
@takluyver

This comment has been minimized.

Show comment
Hide comment
@takluyver

takluyver Dec 15, 2011

Member

We don't want to do it with the JSON - that's assumed to be UTF-8 encoded. This is about making .py files from the notebooks.

Member

takluyver commented Dec 15, 2011

We don't want to do it with the JSON - that's assumed to be UTF-8 encoded. This is about making .py files from the notebooks.

@fperez

This comment has been minimized.

Show comment
Hide comment
@fperez

fperez Dec 15, 2011

Member

@bfroehle, believe it or not, it's the JSON spec that doesn't support comments, period. Fortunately in this case, the issue is about what we wrtie to disk on .py files, as @takluyver points out, so we don't need to worry about JSON for this issue.

Member

fperez commented Dec 15, 2011

@bfroehle, believe it or not, it's the JSON spec that doesn't support comments, period. Fortunately in this case, the issue is about what we wrtie to disk on .py files, as @takluyver points out, so we don't need to worry about JSON for this issue.

@takluyver

This comment has been minimized.

Show comment
Hide comment
@takluyver

takluyver Dec 15, 2011

Member

@bfroehle : snap ;-)

Member

takluyver commented Dec 15, 2011

@bfroehle : snap ;-)

@fperez fperez closed this in 688cc09 Dec 15, 2011

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014

Merge pull request #1158 from takluyver/nbpy-encoding-declaration
Add coding header when notebook exported to .py file.

Closes gh-1156, also strips the encoding declaration out when we load a .py file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment