prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Remove deprecated imp module
  Currently, `mne/commands/utils.py` still uses the deprecated `imp` module, which has long been replaced with `importlib`. According to [this answer on SO](https://stackoverflow.com/a/67692/1112283), the current solution works only on Python 3.5/3.6, and there is a (deprecated) alternative for Python 3.3/3.4. All versions < 3.3 need to use `imp`.
  
  How should this be handled in MNE?
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `mne/commands/utils.py`
  Content:
  ```
  1 """Some utility functions for commands (e.g. for cmdline handling)."""
  2 
  3 # Authors: Yaroslav Halchenko <debian@onerussian.com>
  4 #
  5 # License: BSD (3-clause)
  6 
  7 import imp
  8 import os
  9 import re
  10 from optparse import OptionParser
  11 
  12 import mne
  13 
  14 
  15 def get_optparser(cmdpath, usage=None):
  16     """Create OptionParser with cmd specific settings (e.g. prog value)."""
  17     command = os.path.basename(cmdpath)
  18     if re.match('mne_(.*).py', command):
  19         command = command[4:-3]
  20     elif re.match('mne_(.*).pyc', command):
  21         command = command[4:-4]
  22 
  23     # Fetch description
  24     if cmdpath.endswith('.pyc'):
  25         mod = imp.load_compiled('__temp', cmdpath)
  26     else:
  27         mod = imp.load_source('__temp', cmdpath)
  28     if mod.__doc__:
  29         doc, description, epilog = mod.__doc__, None, None
  30 
  31         doc_lines = doc.split('\n')
  32         description = doc_lines[0]
  33         if len(doc_lines) > 1:
  34             epilog = '\n'.join(doc_lines[1:])
  35 
  36     # monkey patch OptionParser to not wrap epilog
  37     OptionParser.format_epilog = lambda self, formatter: self.epilog
  38     parser = OptionParser(prog="mne %s" % command,
  39                           version=mne.__version__,
  40                           description=description,
  41                           epilog=epilog, usage=usage)
  42 
  43     return parser
  44
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/mne/commands/utils.py b/mne/commands/utils.py
  --- a/mne/commands/utils.py
  +++ b/mne/commands/utils.py
  @@ -4,7 +4,7 @@
   #
   # License: BSD (3-clause)
   
  -import imp
  +import sys
   import os
   import re
   from optparse import OptionParser
  @@ -12,6 +12,42 @@
   import mne
   
   
  +def load_module(name, path):
  +    """Load module from .py/.pyc file.
  +
  +    Parameters
  +    ----------
  +    name : str
  +        Name of the module.
  +    path : str
  +        Path to .py/.pyc file.
  +
  +    Returns
  +    -------
  +    mod : module
  +        Imported module.
  +    """
  +    if sys.version_info < (3, 3):
  +        import imp
  +        if path.endswith('.pyc'):
  +            return imp.load_compiled(name, path)
  +        else:
  +            return imp.load_source(name, path)
  +    elif sys.version_info < (3, 5):
  +        if path.endswith('.pyc'):
  +            from importlib.machinery import SourcelessFileLoader
  +            return SourcelessFileLoader(name, path).load_module()
  +        else:
  +            from importlib.machinery import SourceFileLoader
  +            return SourceFileLoader(name, path).load_module()
  +    else:  # Python 3.5 or greater
  +        from importlib.util import spec_from_file_location, module_from_spec
  +        spec = spec_from_file_location(name, path)
  +        mod = module_from_spec(spec)
  +        spec.loader.exec_module(mod)
  +        return mod
  +
  +
   def get_optparser(cmdpath, usage=None):
       """Create OptionParser with cmd specific settings (e.g. prog value)."""
       command = os.path.basename(cmdpath)
  @@ -21,10 +57,7 @@
           command = command[4:-4]
   
       # Fetch description
  -    if cmdpath.endswith('.pyc'):
  -        mod = imp.load_compiled('__temp', cmdpath)
  -    else:
  -        mod = imp.load_source('__temp', cmdpath)
  +    mod = load_module('__temp', cmdpath)
       if mod.__doc__:
           doc, description, epilog = mod.__doc__, None, None
  
verification_info: {"golden_diff": "diff --git a/mne/commands/utils.py b/mne/commands/utils.py\n--- a/mne/commands/utils.py\n+++ b/mne/commands/utils.py\n@@ -4,7 +4,7 @@\n #\n # License: BSD (3-clause)\n \n-import imp\n+import sys\n import os\n import re\n from optparse import OptionParser\n@@ -12,6 +12,42 @@\n import mne\n \n \n+def load_module(name, path):\n+    \"\"\"Load module from .py/.pyc file.\n+\n+    Parameters\n+    ----------\n+    name : str\n+        Name of the module.\n+    path : str\n+        Path to .py/.pyc file.\n+\n+    Returns\n+    -------\n+    mod : module\n+        Imported module.\n+    \"\"\"\n+    if sys.version_info < (3, 3):\n+        import imp\n+        if path.endswith('.pyc'):\n+            return imp.load_compiled(name, path)\n+        else:\n+            return imp.load_source(name, path)\n+    elif sys.version_info < (3, 5):\n+        if path.endswith('.pyc'):\n+            from importlib.machinery import SourcelessFileLoader\n+            return SourcelessFileLoader(name, path).load_module()\n+        else:\n+            from importlib.machinery import SourceFileLoader\n+            return SourceFileLoader(name, path).load_module()\n+    else:  # Python 3.5 or greater\n+        from importlib.util import spec_from_file_location, module_from_spec\n+        spec = spec_from_file_location(name, path)\n+        mod = module_from_spec(spec)\n+        spec.loader.exec_module(mod)\n+        return mod\n+\n+\n def get_optparser(cmdpath, usage=None):\n     \"\"\"Create OptionParser with cmd specific settings (e.g. prog value).\"\"\"\n     command = os.path.basename(cmdpath)\n@@ -21,10 +57,7 @@\n         command = command[4:-4]\n \n     # Fetch description\n-    if cmdpath.endswith('.pyc'):\n-        mod = imp.load_compiled('__temp', cmdpath)\n-    else:\n-        mod = imp.load_source('__temp', cmdpath)\n+    mod = load_module('__temp', cmdpath)\n     if mod.__doc__:\n         doc, description, epilog = mod.__doc__, None, None\n", "issue": "Remove deprecated imp module\nCurrently, `mne/commands/utils.py` still uses the deprecated `imp` module, which has long been replaced with `importlib`. According to [this answer on SO](https://stackoverflow.com/a/67692/1112283), the current solution works only on Python 3.5/3.6, and there is a (deprecated) alternative for Python 3.3/3.4. All versions < 3.3 need to use `imp`.\r\n\r\nHow should this be handled in MNE?\n", "before_files": [{"content": "\"\"\"Some utility functions for commands (e.g. for cmdline handling).\"\"\"\n\n# Authors: Yaroslav Halchenko <debian@onerussian.com>\n#\n# License: BSD (3-clause)\n\nimport imp\nimport os\nimport re\nfrom optparse import OptionParser\n\nimport mne\n\n\ndef get_optparser(cmdpath, usage=None):\n    \"\"\"Create OptionParser with cmd specific settings (e.g. prog value).\"\"\"\n    command = os.path.basename(cmdpath)\n    if re.match('mne_(.*).py', command):\n        command = command[4:-3]\n    elif re.match('mne_(.*).pyc', command):\n        command = command[4:-4]\n\n    # Fetch description\n    if cmdpath.endswith('.pyc'):\n        mod = imp.load_compiled('__temp', cmdpath)\n    else:\n        mod = imp.load_source('__temp', cmdpath)\n    if mod.__doc__:\n        doc, description, epilog = mod.__doc__, None, None\n\n        doc_lines = doc.split('\\n')\n        description = doc_lines[0]\n        if len(doc_lines) > 1:\n            epilog = '\\n'.join(doc_lines[1:])\n\n    # monkey patch OptionParser to not wrap epilog\n    OptionParser.format_epilog = lambda self, formatter: self.epilog\n    parser = OptionParser(prog=\"mne %s\" % command,\n                          version=mne.__version__,\n                          description=description,\n                          epilog=epilog, usage=usage)\n\n    return parser\n", "path": "mne/commands/utils.py"}], "after_files": [{"content": "\"\"\"Some utility functions for commands (e.g. for cmdline handling).\"\"\"\n\n# Authors: Yaroslav Halchenko <debian@onerussian.com>\n#\n# License: BSD (3-clause)\n\nimport sys\nimport os\nimport re\nfrom optparse import OptionParser\n\nimport mne\n\n\ndef load_module(name, path):\n    \"\"\"Load module from .py/.pyc file.\n\n    Parameters\n    ----------\n    name : str\n        Name of the module.\n    path : str\n        Path to .py/.pyc file.\n\n    Returns\n    -------\n    mod : module\n        Imported module.\n    \"\"\"\n    if sys.version_info < (3, 3):\n        import imp\n        if path.endswith('.pyc'):\n            return imp.load_compiled(name, path)\n        else:\n            return imp.load_source(name, path)\n    elif sys.version_info < (3, 5):\n        if path.endswith('.pyc'):\n            from importlib.machinery import SourcelessFileLoader\n            return SourcelessFileLoader(name, path).load_module()\n        else:\n            from importlib.machinery import SourceFileLoader\n            return SourceFileLoader(name, path).load_module()\n    else:  # Python 3.5 or greater\n        from importlib.util import spec_from_file_location, module_from_spec\n        spec = spec_from_file_location(name, path)\n        mod = module_from_spec(spec)\n        spec.loader.exec_module(mod)\n        return mod\n\n\ndef get_optparser(cmdpath, usage=None):\n    \"\"\"Create OptionParser with cmd specific settings (e.g. prog value).\"\"\"\n    command = os.path.basename(cmdpath)\n    if re.match('mne_(.*).py', command):\n        command = command[4:-3]\n    elif re.match('mne_(.*).pyc', command):\n        command = command[4:-4]\n\n    # Fetch description\n    mod = load_module('__temp', cmdpath)\n    if mod.__doc__:\n        doc, description, epilog = mod.__doc__, None, None\n\n        doc_lines = doc.split('\\n')\n        description = doc_lines[0]\n        if len(doc_lines) > 1:\n            epilog = '\\n'.join(doc_lines[1:])\n\n    # monkey patch OptionParser to not wrap epilog\n    OptionParser.format_epilog = lambda self, formatter: self.epilog\n    parser = OptionParser(prog=\"mne %s\" % command,\n                          version=mne.__version__,\n                          description=description,\n                          epilog=epilog, usage=usage)\n\n    return parser\n", "path": "mne/commands/utils.py"}]}