Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Add encoding header to Python files downloaded from notebooks. #1159

wants to merge 1 commit into from

3 participants


Closes gh-1156.


This prevents the encoding header from appearing in the notebook interface. Is this the desired behavior?


Snap: PR #1158. ;-)

I'm pretty sure it is the desired behaviour - we execute code as unicode strings, and Python complains if it gets an encoding declaration in a unicode string. But we should remove it consistently (I've done it using the regex from PEP 263).


Wow, we have dueling PRs! Neat!! BTW, we're on IRC right now if you guys want to jump in..

@bfroehle bfroehle closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 1 deletion.
  1. +5 −1 IPython/nbformat/v2/
6 IPython/nbformat/v2/
@@ -40,6 +40,8 @@ def to_notebook(self, s, **kwargs):
for line in lines:
if line.startswith(u'# <nbformat>'):
+ elif line.startswith(u'# -*-'):
+ pass
elif line.startswith(u'# <codecell>'):
cell = self.new_cell(state, cell_lines)
if cell is not None:
@@ -111,7 +113,9 @@ class PyWriter(NotebookWriter):
def writes(self, nb, **kwargs):
lines = []
- lines.extend([u'# <nbformat>2</nbformat>',''])
+ lines.extend([u'# -*- coding: utf-8 -*-',
+ u'# <nbformat>2</nbformat>',
+ u''])
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type == u'code':
Something went wrong with that request. Please try again.