Repository: mne-tools/mne-python

Issue:
subprocess env is not set properly
While running `mne.bem.mne_make_watershed_bem` I ran into a unspecific error:
`Error: io.UnsupportedOperation: fileno`

which is due to a subprocess error triggered earlier ('mri_watershed not found'). 

I tried changing FREESURFER_HOME via `mne.set_config` without luck.

I manually changed `run_subprocess`, as follow, which fixed the issue.

```python
def run_subprocess(command, return_code=False, verbose=None, *args, **kwargs):    
    import subprocess, os
    my_env = os.environ.copy()
    my_env["PATH"] = "/usr/sbin:/sbin:" + my_env["PATH"]

    cp = subprocess.run(command, 
                        env=my_env,
                        universal_newlines=True, 
                        stdout=subprocess.PIPE, 
                        stderr=subprocess.PIPE)
    return cp.stdout, cp.stderr
```

(And if someone opens a PR, you can pass the missing overwrite param here https://github.com/mne-tools/mne-python/blob/master/mne/bem.py#L1156)


Discussion:
you hacked the path?

this function looks very different from
https://github.com/mne-tools/mne-python/blob/master/mne/utils/misc.py#L85

are you using latest release/master?

> you hacked the path?

In the fix yes, else no.

> this function looks very different from

Yes, I went for the simplest possible fix ;)

This solved the issue for me, but I don't want to open a PR, because it's not a pure-python, platform-independent, easy-to-debug issue.

> are you using latest release/master?

yes
@larsoner any thought? It's hard to integrate a fix like this unless we can
replicate...

>


Resolving patch:
diff --git a/mne/bem.py b/mne/bem.py
--- a/mne/bem.py
+++ b/mne/bem.py
@@ -31,7 +31,7 @@
                       _fast_cross_nd_sum, _get_solids)
 from .transforms import _ensure_trans, apply_trans, Transform
 from .utils import (verbose, logger, run_subprocess, get_subjects_dir, warn,
-                    _pl, _validate_type, _TempDir)
+                    _pl, _validate_type, _TempDir, get_config)
 from .fixes import einsum
 
 
@@ -1153,7 +1153,8 @@ def make_watershed_bem(subject, subjects_dir=None, overwrite=False,
                                                  read_metadata=True)
             volume_info.update(new_info)  # replace volume info, 'head' stays
 
-            write_surface(s, rr, tris, volume_info=volume_info)
+            write_surface(s, rr, tris, volume_info=volume_info,
+                          overwrite=overwrite)
             # Create symbolic links
             surf_out = op.join(bem_dir, '%s.surf' % s)
             if not overwrite and op.exists(surf_out):
@@ -1563,7 +1564,8 @@ def write_bem_solution(fname, bem):
 def _prepare_env(subject, subjects_dir, requires_freesurfer):
     """Prepare an env object for subprocess calls."""
     env = os.environ.copy()
-    if requires_freesurfer and not os.environ.get('FREESURFER_HOME'):
+    fs_home = get_config('FREESURFER_HOME')
+    if fs_home is None:
         raise RuntimeError('I cannot find freesurfer. The FREESURFER_HOME '
                            'environment variable is not set.')
 
@@ -1577,8 +1579,8 @@ def _prepare_env(subject, subjects_dir, requires_freesurfer):
     if not op.isdir(subject_dir):
         raise RuntimeError('Could not find the subject data directory "%s"'
                            % (subject_dir,))
-    env['SUBJECT'] = subject
-    env['SUBJECTS_DIR'] = subjects_dir
+    env.update(SUBJECT=subject, SUBJECTS_DIR=subjects_dir,
+               FREESURFER_HOME=fs_home)
     mri_dir = op.join(subject_dir, 'mri')
     bem_dir = op.join(subject_dir, 'bem')
     return env, mri_dir, bem_dir


Test patch: