diff --git a/kwant/tests/test_plotter.py b/kwant/tests/test_plotter.py
index 4bf6a510ac41f4937bec8275330ee51a6af3383f..4619116d990ebe2516c695e4982c63e907be607c 100644
--- a/kwant/tests/test_plotter.py
+++ b/kwant/tests/test_plotter.py
@@ -31,10 +31,10 @@ def test_importable_without_matplotlib():
         suffix = 'py'
     assert suffix == 'py'
     fname = sep.join((prefix, suffix))
-    with open(fname) as f:
+    with open(fname, 'rb') as f:
         code = f.read()
-    code = code.replace('from . import', 'from kwant import')
-    code = code.replace('matplotlib', 'totalblimp')
+    code = code.replace(b'from . import', b'from kwant import')
+    code = code.replace(b'matplotlib', b'totalblimp')
 
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always")
@@ -253,7 +253,7 @@ def syst_rect(lat, salt, W=3, L=50):
     ww = W//2
 
     def onsite(site):
-        return 4 + 0.1 * kwant.digest.gauss(site.tag, salt=salt)
+        return 4 + 0.1 * kwant.digest.gauss(repr(site.tag), salt=salt)
 
     syst[(lat(i, j) for i in range(-ll, ll+1)
          for j in range(-ww, ww+1))] = onsite
diff --git a/kwant/version.py b/kwant/version.py
index 70e6b4d8185f7c31861baa809956a81af6dc1bfc..c7da0e294a86a7c242be1681b01bca6a20f705cd 100644
--- a/kwant/version.py
+++ b/kwant/version.py
@@ -86,7 +86,7 @@ def get_version_from_git():
 def init(version_file='_kwant_version.py'):
     global version, version_is_from_git
     version_info = {}
-    with open(os.path.join(package_root, version_file), 'r') as f:
+    with open(os.path.join(package_root, version_file), 'rb') as f:
         exec(f.read(), {}, version_info)
     version = version_info['version']
     version_is_from_git = (version == "__use_git__")
diff --git a/setup.py b/setup.py
index f3268a2abf4bbf5a1bc34aed30b2810528fff5da..5888879be93179e814e0a8e73d696b5f6f97b79a 100755
--- a/setup.py
+++ b/setup.py
@@ -243,10 +243,10 @@ class build_tut(Command):
             os.mkdir(tut_dir)
         for in_fname in glob.glob('doc/source/tutorial/*.py'):
             out_fname = os.path.join(tut_dir, os.path.basename(in_fname))
-            with open(in_fname) as in_file:
-                with open(out_fname, 'w') as out_file:
+            with open(in_fname, 'rb') as in_file:
+                with open(out_fname, 'wb') as out_file:
                     for line in in_file:
-                        if not line.startswith('#HIDDEN'):
+                        if not line.startswith(b'#HIDDEN'):
                             out_file.write(line)
 
 
@@ -364,7 +364,7 @@ def write_version(fname):
 def long_description():
     text = []
     try:
-        with open('README.rst') as f:
+        with open('README.rst', encoding='utf8') as f:
             for line in f:
                 if line.startswith('See also in this directory:'):
                     break