diff --git a/lib/utils/printf.c b/lib/utils/printf.c
index 082b1c65a89657e4732dd4b68d70f2c178a25cc5..6dce8969414e9be9bd4ac24a920c364541d3c93c 100644
--- a/lib/utils/printf.c
+++ b/lib/utils/printf.c
@@ -51,7 +51,14 @@ int vprintf(const char *fmt, va_list ap) {
 int DEBUG_printf(const char *fmt, ...) {
     va_list ap;
     va_start(ap, fmt);
+    #if MICROPY_DEBUG_STDERR
+    // Printing debug to stderr may give a chance tests which
+    // check stdout to pass, etc.
+    extern const mp_print_t mp_stderr_print;
+    int ret = mp_vprintf(&mp_stderr_print, fmt, ap);
+    #else
     int ret = mp_vprintf(&mp_plat_print, fmt, ap);
+    #endif
     va_end(ap);
     return ret;
 }
diff --git a/unix/Makefile b/unix/Makefile
index 2654c23fd50f892322ffc9f30a39bd9153e6eb12..677c3b4fde2a4b3846dd6ac4812c8a7e4a68f8bc 100644
--- a/unix/Makefile
+++ b/unix/Makefile
@@ -129,6 +129,7 @@ endif
 
 LIB_SRC_C = $(addprefix lib/,\
 	$(LIB_SRC_C_EXTRA) \
+	utils/printf.c \
 	)
 
 OBJ = $(PY_O)
diff --git a/unix/main.c b/unix/main.c
index 26618af3f7ed553e8e397ed62d7969c9d0eb8dab..741568f75321e3026cac4548481cad71a6b1b56d 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -574,14 +574,6 @@ uint mp_import_stat(const char *path) {
     return MP_IMPORT_STAT_NO_EXIST;
 }
 
-int DEBUG_printf(const char *fmt, ...) {
-    va_list ap;
-    va_start(ap, fmt);
-    int ret = mp_vprintf(&mp_stderr_print, fmt, ap);
-    va_end(ap);
-    return ret;
-}
-
 void nlr_jump_fail(void *val) {
     printf("FATAL: uncaught NLR %p\n", val);
     exit(1);
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index 3d666d0221311e618895e4a013aac7a17267ee40..8ff4e096d0a7b53c69e72591f517e0d0e72df7de 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -50,6 +50,7 @@
 #define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1)
 #define MICROPY_MEM_STATS           (1)
 #define MICROPY_DEBUG_PRINTERS      (1)
+#define MICROPY_DEBUG_STDERR        (1)
 #define MICROPY_USE_READLINE_HISTORY (1)
 #define MICROPY_HELPER_REPL         (1)
 #define MICROPY_REPL_EMACS_KEYS     (1)