diff --git a/python/plumed.pyx b/python/plumed.pyx
index a9b9fc0766c939561bf11ec46929161abaabc68b..e717616360586feea4f87be3a6ca5a67ab7e6a67 100644
--- a/python/plumed.pyx
+++ b/python/plumed.pyx
@@ -32,7 +32,7 @@ import array
 try:
      import numpy as np
      HAS_NUMPY=True
-except:
+except ImportError:
      HAS_NUMPY=False
 
 cdef class Plumed:
diff --git a/python/setup.py b/python/setup.py
index 929c3af126e017066f1af718d89b08966c5319d1..45a824491fc1e2e5a3f34a74d8aed1d1998e373e 100644
--- a/python/setup.py
+++ b/python/setup.py
@@ -79,7 +79,7 @@ def readme():
 
 try:
     include_dirs=[os.environ["plumed_include_dir"]]
-except:
+except KeyError:
     include_dirs=["."]
 
 # allow one to force using cython with env var plumed_force_cython=yes
@@ -88,7 +88,7 @@ try:
     if(os.environ["plumed_force_cython"]=="yes"):
         print('plumed_force_cython=yes')
         USE_CYTHON = True
-except:
+except KeyError:
     pass
 
 # if plumed.cpp is available, do not need cython
@@ -103,7 +103,7 @@ if USE_CYTHON:
         print('importing cython')
         from Cython.Build import cythonize
         extension="pyx"
-    except:
+    except ImportError:
         print('Error: building ' + plumedname + ' requires cython. Please install it first with pip install cython')
         sys.exit(-1)
 else: