Repository: mne-tools/mne-python

Issue:
import mne error
Hi 
I've been working using mne for past few months and am having an issue importing mne and the co libraries from yesterday.
I am not sure if it's mne issue or a common one
Please help me out here
![error](https://cloud.githubusercontent.com/assets/6408125/11994860/2059b7d0-aa6c-11e5-981a-0609d732b182.JPG)



Discussion:
What version of Python are you running? 

It's weird that the problem would just have started because that code is a couple of years old (and mostly unchanged) I think. Did you upgrade packages or Python interpreter around the time things stopped working? 

Anaconda 2.4.0
I have not done any changes recently.
I tried upgrading and re installing MNE but still no luck

are you able to import other libraries? `import numpy` or `import scipy` -- do they work?

I suspect your config file is corrupted.

Try removing it.

It's in $HOME/.mne/mne-python.json

@jasmainak  yes I am able to install other libraries 
@agramfort  I removed json file and now am getting this error
![error](https://cloud.githubusercontent.com/assets/6408125/11996303/fdab4a5e-aa85-11e5-9c95-3a380df76d88.JPG)

I'll reinstall python and check 

remove everything and run again a "pip install mne"

Already tried
no luck

Do you install with pip?

> On 24 déc. 2015, at 17:09, ahibahi notifications@github.com wrote:
> 
> Already tried
> no luck
> 
> —
> Reply to this email directly or view it on GitHub.

Yes 
I reinstalled python too
still getting the same error

in ipython do:

import mne
mne

it will point you where mne is installed. Remove the folder and reinstall.

if it breaks make sure you did not name a file with a name similar to an
mne file.

HTH

Closing due to inactivity, @ahibahi feel free to reopen if you are still having trouble or have new insights

@ahibahi did you ever resolve this?  i have the exact same error and i've been stuck on it for weeks!

@Eric89GXL @agramfort.  I'm also getting this error.  I followed @agramfort's advice removing the config file: $HOME/.mne/mne-python.json.  The package loaded successfully, but seems to depend on that file.  Any ideas for how to regenerate it or replace it?

We could try to be more careful when reading the file, and warn if it's corrupted here:

https://github.com/mne-tools/mne-python/blob/master/mne/utils.py#L1258

+1 for being more careful and avoid a crash

On Wed, Jun 15, 2016 at 9:38 PM +0200, "Eric Larson" notifications@github.com wrote:

We could try to be more careful when reading the file, and warn if it's corrupted here:

https://github.com/mne-tools/mne-python/blob/master/mne/utils.py#L1258

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

is the $HOME/.mne/mne-python.json file supposed to be generated on install? I uninstalled mne and reinstalled but I don't see the config file

It should only be generated when saving a config value to disk, I think.


Resolving patch:
diff --git a/mne/utils.py b/mne/utils.py
--- a/mne/utils.py
+++ b/mne/utils.py
@@ -1214,6 +1214,22 @@ def set_memmap_min_size(memmap_min_size):
 )
 
 
+def _load_config(config_path, raise_error=False):
+    """Helper to safely load a config file"""
+    with open(config_path, 'r') as fid:
+        try:
+            config = json.load(fid)
+        except ValueError:
+            # No JSON object could be decoded --> corrupt file?
+            msg = ('The MNE-Python config file (%s) is not a valid JSON '
+                   'file and might be corrupted' % config_path)
+            if raise_error:
+                raise RuntimeError(msg)
+            warn(msg)
+            config = dict()
+    return config
+
+
 def get_config(key=None, default=None, raise_error=False, home_dir=None):
     """Read mne(-python) preference from env, then mne-python config
 
@@ -1255,13 +1271,11 @@ def get_config(key=None, default=None, raise_error=False, home_dir=None):
         key_found = False
         val = default
     else:
-        with open(config_path, 'r') as fid:
-            config = json.load(fid)
-            if key is None:
-                return config
+        config = _load_config(config_path)
+        if key is None:
+            return config
         key_found = key in config
         val = config.get(key, default)
-
     if not key_found and raise_error is True:
         meth_1 = 'os.environ["%s"] = VALUE' % key
         meth_2 = 'mne.utils.set_config("%s", VALUE)' % key
@@ -1310,8 +1324,7 @@ def set_config(key, value, home_dir=None):
     # Read all previous values
     config_path = get_config_path(home_dir=home_dir)
     if op.isfile(config_path):
-        with open(config_path, 'r') as fid:
-            config = json.load(fid)
+        config = _load_config(config_path, raise_error=True)
     else:
         config = dict()
         logger.info('Attempting to create new mne-python configuration '


Test patch:
diff --git a/mne/tests/test_utils.py b/mne/tests/test_utils.py
--- a/mne/tests/test_utils.py
+++ b/mne/tests/test_utils.py
@@ -21,7 +21,7 @@
                        set_memmap_min_size, _get_stim_channel, _check_fname,
                        create_slices, _time_mask, random_permutation,
                        _get_call_line, compute_corr, sys_info, verbose,
-                       check_fname, requires_ftp)
+                       check_fname, requires_ftp, get_config_path)
 
 
 warnings.simplefilter('always')  # enable b/c these tests throw warnings
@@ -354,6 +354,15 @@ def test_config():
         warnings.simplefilter('always')
         set_config(key, value, home_dir=tempdir)
     assert_equal(get_config(home_dir=tempdir), config)
+    # Check what happens when we use a corrupted file
+    json_fname = get_config_path(home_dir=tempdir)
+    with open(json_fname, 'w') as fid:
+        fid.write('foo{}')
+    with warnings.catch_warnings(record=True) as w:
+        assert_equal(get_config(home_dir=tempdir), dict())
+    assert_true(any('not a valid JSON' in str(ww.message) for ww in w))
+    with warnings.catch_warnings(record=True) as w:  # non-standard key
+        assert_raises(RuntimeError, set_config, key, 'true', home_dir=tempdir)
 
 
 def test_show_fiff():