Skip to content

Instantly share code, notes, and snippets.

@jdunck
Last active August 17, 2018 20:29
Show Gist options
  • Save jdunck/87fbb94cb737ab6b65e4 to your computer and use it in GitHub Desktop.
Save jdunck/87fbb94cb737ab6b65e4 to your computer and use it in GitHub Desktop.
Python preamble ordering

It's been hard for me to remember what the required relative order of python preambles (that is, encoding markers, future imports, and module docstrings) should be.

This is an empirical approach, showing that the proper order is:

# -*- coding: utf8 -*-

"""døcst®îñg"""

from __future__ import unicode_literals
# -*- coding: utf8 -*-
"""døcst®îñg"""
from __future__ import unicode_literals
# -*- coding: utf8 -*-
from __future__ import unicode_literals
"""døcst®îñg"""
from __future__ import unicode_literals
# -*- coding: utf8 -*-
"""døcst®îñg"""
import importlib
for module_name in ['encoding_docstring_future', 'encoding_future_docstring', 'future_encoding_docstring']:
try:
module = importlib.import_module(module_name)
except (ImportError, SyntaxError):
print "Failed to load {0}".format(module_name)
continue
print module_name, module.__doc__
@jdunck
Copy link
Author

jdunck commented Oct 27, 2015

$ python runner.py
encoding_docstring_future døcst®îñg
encoding_future_docstring None
Failed to load future_encoding_docstring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment