prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  gui import from psychopy not woriking
  Hi all,
  
  im trying to run a psychopy script from terminal but I get this error:
  
  Traceback (most recent call last):
    File "nf_test_lastrun.py", line 11, in <module>
      from psychopy import visual, core, data, event, logging, sound, gui
    File "/Library/Python/2.7/site-packages/PsychoPy-1.81.00-py2.7.egg/psychopy/gui.py", line 11, in <module>
      from psychopy.app import localization
    File "/Library/Python/2.7/site-packages/PsychoPy-1.81.00-py2.7.egg/psychopy/app/localization/**init**.py", line 89, in <module>
      languageID, lang = getID()
    File "/Library/Python/2.7/site-packages/PsychoPy-1.81.00-py2.7.egg/psychopy/app/localization/**init**.py", line 78, in getID
      val = codeFromWxId[wx.LANGUAGE_DEFAULT]
  KeyError: 0
  
  when I open python and try to import from python, all work but gui.
  
  any suggestions
  thanks
  clemens
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `psychopy/app/localization/__init__.py`
  Content:
  ```
  1 #!/usr/bin/env python2
  2 # -*- coding: utf-8 -*-
  3 
  4 """Language localization for PsychoPy.
  5 
  6 Sets the locale value as a wx languageID (int) and initializes gettext translation _translate():
  7     from psychopy.app import localization
  8 """
  9 
  10 # Part of the PsychoPy library
  11 # Copyright (C) 2014 Jonathan Peirce
  12 # Distributed under the terms of the GNU General Public License (GPL).
  13 
  14 # Author: Jeremy Gray, July 2014
  15 
  16 
  17 import gettext
  18 import os, sys, glob, codecs
  19 from psychopy import logging, prefs
  20 
  21 import wx
  22 
  23 # need a wx App for wx.Locale:
  24 try:
  25     wx.Dialog(None, -1)
  26 except wx._core.PyNoAppError:
  27     if wx.version() < '2.9':
  28         tmpApp = wx.PySimpleApp()
  29     else:
  30         tmpApp = wx.App(False)
  31 
  32 # Get a dict of locale aliases from wx.Locale() -- same cross-platform (Win 7, Mac 10.9)
  33 locale = wx.Locale()
  34 aliases = {}
  35 wxIdFromCode = {}  # int: 0, 2-229
  36 codeFromWxId = {}  # used in directory names e.g. ja_JP; never JPN ala Windows
  37 winmap = {}  # get windows 3-letter code (=val) from canonical form (=key); use only for setting locale (non-wx)
  38 locname = {}  # descriptive name, if available; 5-letter code if not
  39 reverseMap = {}
  40 
  41 for i in range(230):
  42     info = locale.GetLanguageInfo(i)
  43     if info:
  44         aliases[info.Description] = info.CanonicalName  # mix of forms: ja or ja_JP
  45         wxIdFromCode[info.CanonicalName] = i
  46         codeFromWxId[i] = info.CanonicalName
  47 
  48 mappings = os.path.join(os.path.dirname(__file__), 'mappings.txt')
  49 for line in codecs.open(mappings, 'rU', 'utf8').readlines():
  50     try:
  51         can, win, name = line.strip().split(' ', 2)  # canonical, windows, name-with-spaces
  52     except ValueError:
  53         can, win = line.strip().split(' ', 1)
  54         name = can
  55     winmap[can] = win
  56     locname[can] = name
  57     reverseMap[name] = can
  58 
  59 # what are the available translations? available languages on the OS?
  60 expr = os.path.join(os.path.dirname(__file__), '..', 'locale', '*')
  61 available = sorted(map(os.path.basename, glob.glob(expr)))
  62 sysAvail = [str(l) for l in codeFromWxId.values()  # installed language packs
  63             if l and locale.IsAvailable(wxIdFromCode[l])]
  64 
  65 def getID(lang=None):
  66     """Get wx ID of language to use for translations: `lang`, pref, or system default.
  67 
  68     `lang` is a 5 char `language_REGION`, eg ja_JP
  69     """
  70     if lang:
  71         val = lang
  72     else:
  73         try:
  74             val = prefs.app['locale']
  75         except KeyError:
  76             val = locale.GetLocale()  # wx.Locale, no encoding
  77         if not val:
  78             val = codeFromWxId[wx.LANGUAGE_DEFAULT]
  79     try:
  80         # out-dated: [can't set wx.Locale here because no app yet] now there is an app
  81         # here just determine the value to be used when it can be set
  82         language = wxIdFromCode[val]
  83     except KeyError:
  84         logging.error('locale %s not known to wx.Locale, using default' % val)
  85         language = wx.LANGUAGE_DEFAULT
  86 
  87     return language, val
  88 
  89 languageID, lang = getID()
  90 #use lang like this:
  91 #import locale  -- the non-wx version of locale
  92 #
  93 #if sys.platform.startswith('win'):
  94 #        v = winmap[val]
  95 #else: v=val
  96 #locale.setlocale(locale.LC_ALL, (v, 'UTF-8'))
  97 
  98 # set locale before splash screen:
  99 if locale.IsAvailable(languageID):
  100     wxlocale = wx.Locale(languageID)
  101 else:
  102     wxlocale = wx.Locale(wx.LANGUAGE_DEFAULT)
  103 
  104 # ideally rewrite the following using wxlocale only:
  105 path = os.path.join(os.path.dirname(__file__), '..', 'locale', lang, 'LC_MESSAGE') + os.sep
  106 mofile = os.path.join(path, 'messages.mo')
  107 try:
  108     logging.debug("Opening message catalog %s for locale %s" % (mofile, lang))
  109     trans = gettext.GNUTranslations(open(mofile, "rb"))
  110 except IOError:
  111     logging.debug("Locale for '%s' not found. Using default." % lang)
  112     trans = gettext.NullTranslations()
  113 trans.install(unicode=True)
  114 
  115 # to avoid a crash, PsychoPy app uses a nonstandard name _translate instead of _
  116 # seems like a var in a dependency is named _, clobbering _ as global translation:
  117 __builtins__['_translate'] = _
  118 del(__builtins__['_'])  # idea: force psychopy code to use _translate
  119 
  120 
  121 #__builtins__['_'] = wx.GetTranslation
  122 # this seems to have no effect, needs more investigation:
  123 #path = os.path.join(os.path.dirname(__file__), '..', 'locale', lang, 'LC_MESSAGE') + os.sep
  124 #wxlocale.AddCatalogLookupPathPrefix(path)
  125
  ```
  --- 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/psychopy/app/localization/__init__.py b/psychopy/app/localization/__init__.py
  --- a/psychopy/app/localization/__init__.py
  +++ b/psychopy/app/localization/__init__.py
  @@ -31,13 +31,10 @@
   
   # Get a dict of locale aliases from wx.Locale() -- same cross-platform (Win 7, Mac 10.9)
   locale = wx.Locale()
  -aliases = {}
  -wxIdFromCode = {}  # int: 0, 2-229
  -codeFromWxId = {}  # used in directory names e.g. ja_JP; never JPN ala Windows
  -winmap = {}  # get windows 3-letter code (=val) from canonical form (=key); use only for setting locale (non-wx)
  -locname = {}  # descriptive name, if available; 5-letter code if not
  -reverseMap = {}
  -
  +aliases = {u'English (U.S.)': 'en_US'}
  +# set defaults because locale.GetLanguageInfo(0) can return None on some systems:
  +wxIdFromCode = {'en_US': wx.LANGUAGE_DEFAULT}  # int: 0 default, 2-229
  +codeFromWxId = {wx.LANGUAGE_DEFAULT: 'en_US'}  # used in directory names e.g. ja_JP; never JPN ala Windows
   for i in range(230):
       info = locale.GetLanguageInfo(i)
       if info:
  @@ -45,6 +42,10 @@
           wxIdFromCode[info.CanonicalName] = i
           codeFromWxId[i] = info.CanonicalName
   
  +# read all known mappings cross-platform from a file:
  +winmap = {'en_US': 'ENU'}  # get windows 3-letter code (=val) from canonical form (=key); use only for setting locale (non-wx)
  +locname = {'en_US': u'English (U.S.)'}  # descriptive name, if available; 5-letter code if not
  +reverseMap = {u'English (U.S.)': 'en_US'}
   mappings = os.path.join(os.path.dirname(__file__), 'mappings.txt')
   for line in codecs.open(mappings, 'rU', 'utf8').readlines():
       try:
  
verification_info: {"golden_diff": "diff --git a/psychopy/app/localization/__init__.py b/psychopy/app/localization/__init__.py\n--- a/psychopy/app/localization/__init__.py\n+++ b/psychopy/app/localization/__init__.py\n@@ -31,13 +31,10 @@\n \n # Get a dict of locale aliases from wx.Locale() -- same cross-platform (Win 7, Mac 10.9)\n locale = wx.Locale()\n-aliases = {}\n-wxIdFromCode = {}  # int: 0, 2-229\n-codeFromWxId = {}  # used in directory names e.g. ja_JP; never JPN ala Windows\n-winmap = {}  # get windows 3-letter code (=val) from canonical form (=key); use only for setting locale (non-wx)\n-locname = {}  # descriptive name, if available; 5-letter code if not\n-reverseMap = {}\n-\n+aliases = {u'English (U.S.)': 'en_US'}\n+# set defaults because locale.GetLanguageInfo(0) can return None on some systems:\n+wxIdFromCode = {'en_US': wx.LANGUAGE_DEFAULT}  # int: 0 default, 2-229\n+codeFromWxId = {wx.LANGUAGE_DEFAULT: 'en_US'}  # used in directory names e.g. ja_JP; never JPN ala Windows\n for i in range(230):\n     info = locale.GetLanguageInfo(i)\n     if info:\n@@ -45,6 +42,10 @@\n         wxIdFromCode[info.CanonicalName] = i\n         codeFromWxId[i] = info.CanonicalName\n \n+# read all known mappings cross-platform from a file:\n+winmap = {'en_US': 'ENU'}  # get windows 3-letter code (=val) from canonical form (=key); use only for setting locale (non-wx)\n+locname = {'en_US': u'English (U.S.)'}  # descriptive name, if available; 5-letter code if not\n+reverseMap = {u'English (U.S.)': 'en_US'}\n mappings = os.path.join(os.path.dirname(__file__), 'mappings.txt')\n for line in codecs.open(mappings, 'rU', 'utf8').readlines():\n     try:\n", "issue": "gui import from psychopy not woriking\nHi all,\n\nim trying to run a psychopy script from terminal but I get this error:\n\nTraceback (most recent call last):\n  File \"nf_test_lastrun.py\", line 11, in <module>\n    from psychopy import visual, core, data, event, logging, sound, gui\n  File \"/Library/Python/2.7/site-packages/PsychoPy-1.81.00-py2.7.egg/psychopy/gui.py\", line 11, in <module>\n    from psychopy.app import localization\n  File \"/Library/Python/2.7/site-packages/PsychoPy-1.81.00-py2.7.egg/psychopy/app/localization/**init**.py\", line 89, in <module>\n    languageID, lang = getID()\n  File \"/Library/Python/2.7/site-packages/PsychoPy-1.81.00-py2.7.egg/psychopy/app/localization/**init**.py\", line 78, in getID\n    val = codeFromWxId[wx.LANGUAGE_DEFAULT]\nKeyError: 0\n\nwhen I open python and try to import from python, all work but gui.\n\nany suggestions\nthanks\nclemens\n\n", "before_files": [{"content": "#!/usr/bin/env python2\n# -*- coding: utf-8 -*-\n\n\"\"\"Language localization for PsychoPy.\n\nSets the locale value as a wx languageID (int) and initializes gettext translation _translate():\n    from psychopy.app import localization\n\"\"\"\n\n# Part of the PsychoPy library\n# Copyright (C) 2014 Jonathan Peirce\n# Distributed under the terms of the GNU General Public License (GPL).\n\n# Author: Jeremy Gray, July 2014\n\n\nimport gettext\nimport os, sys, glob, codecs\nfrom psychopy import logging, prefs\n\nimport wx\n\n# need a wx App for wx.Locale:\ntry:\n    wx.Dialog(None, -1)\nexcept wx._core.PyNoAppError:\n    if wx.version() < '2.9':\n        tmpApp = wx.PySimpleApp()\n    else:\n        tmpApp = wx.App(False)\n\n# Get a dict of locale aliases from wx.Locale() -- same cross-platform (Win 7, Mac 10.9)\nlocale = wx.Locale()\naliases = {}\nwxIdFromCode = {}  # int: 0, 2-229\ncodeFromWxId = {}  # used in directory names e.g. ja_JP; never JPN ala Windows\nwinmap = {}  # get windows 3-letter code (=val) from canonical form (=key); use only for setting locale (non-wx)\nlocname = {}  # descriptive name, if available; 5-letter code if not\nreverseMap = {}\n\nfor i in range(230):\n    info = locale.GetLanguageInfo(i)\n    if info:\n        aliases[info.Description] = info.CanonicalName  # mix of forms: ja or ja_JP\n        wxIdFromCode[info.CanonicalName] = i\n        codeFromWxId[i] = info.CanonicalName\n\nmappings = os.path.join(os.path.dirname(__file__), 'mappings.txt')\nfor line in codecs.open(mappings, 'rU', 'utf8').readlines():\n    try:\n        can, win, name = line.strip().split(' ', 2)  # canonical, windows, name-with-spaces\n    except ValueError:\n        can, win = line.strip().split(' ', 1)\n        name = can\n    winmap[can] = win\n    locname[can] = name\n    reverseMap[name] = can\n\n# what are the available translations? available languages on the OS?\nexpr = os.path.join(os.path.dirname(__file__), '..', 'locale', '*')\navailable = sorted(map(os.path.basename, glob.glob(expr)))\nsysAvail = [str(l) for l in codeFromWxId.values()  # installed language packs\n            if l and locale.IsAvailable(wxIdFromCode[l])]\n\ndef getID(lang=None):\n    \"\"\"Get wx ID of language to use for translations: `lang`, pref, or system default.\n\n    `lang` is a 5 char `language_REGION`, eg ja_JP\n    \"\"\"\n    if lang:\n        val = lang\n    else:\n        try:\n            val = prefs.app['locale']\n        except KeyError:\n            val = locale.GetLocale()  # wx.Locale, no encoding\n        if not val:\n            val = codeFromWxId[wx.LANGUAGE_DEFAULT]\n    try:\n        # out-dated: [can't set wx.Locale here because no app yet] now there is an app\n        # here just determine the value to be used when it can be set\n        language = wxIdFromCode[val]\n    except KeyError:\n        logging.error('locale %s not known to wx.Locale, using default' % val)\n        language = wx.LANGUAGE_DEFAULT\n\n    return language, val\n\nlanguageID, lang = getID()\n#use lang like this:\n#import locale  -- the non-wx version of locale\n#\n#if sys.platform.startswith('win'):\n#        v = winmap[val]\n#else: v=val\n#locale.setlocale(locale.LC_ALL, (v, 'UTF-8'))\n\n# set locale before splash screen:\nif locale.IsAvailable(languageID):\n    wxlocale = wx.Locale(languageID)\nelse:\n    wxlocale = wx.Locale(wx.LANGUAGE_DEFAULT)\n\n# ideally rewrite the following using wxlocale only:\npath = os.path.join(os.path.dirname(__file__), '..', 'locale', lang, 'LC_MESSAGE') + os.sep\nmofile = os.path.join(path, 'messages.mo')\ntry:\n    logging.debug(\"Opening message catalog %s for locale %s\" % (mofile, lang))\n    trans = gettext.GNUTranslations(open(mofile, \"rb\"))\nexcept IOError:\n    logging.debug(\"Locale for '%s' not found. Using default.\" % lang)\n    trans = gettext.NullTranslations()\ntrans.install(unicode=True)\n\n# to avoid a crash, PsychoPy app uses a nonstandard name _translate instead of _\n# seems like a var in a dependency is named _, clobbering _ as global translation:\n__builtins__['_translate'] = _\ndel(__builtins__['_'])  # idea: force psychopy code to use _translate\n\n\n#__builtins__['_'] = wx.GetTranslation\n# this seems to have no effect, needs more investigation:\n#path = os.path.join(os.path.dirname(__file__), '..', 'locale', lang, 'LC_MESSAGE') + os.sep\n#wxlocale.AddCatalogLookupPathPrefix(path)\n", "path": "psychopy/app/localization/__init__.py"}], "after_files": [{"content": "#!/usr/bin/env python2\n# -*- coding: utf-8 -*-\n\n\"\"\"Language localization for PsychoPy.\n\nSets the locale value as a wx languageID (int) and initializes gettext translation _translate():\n    from psychopy.app import localization\n\"\"\"\n\n# Part of the PsychoPy library\n# Copyright (C) 2014 Jonathan Peirce\n# Distributed under the terms of the GNU General Public License (GPL).\n\n# Author: Jeremy Gray, July 2014\n\n\nimport gettext\nimport os, sys, glob, codecs\nfrom psychopy import logging, prefs\n\nimport wx\n\n# need a wx App for wx.Locale:\ntry:\n    wx.Dialog(None, -1)\nexcept wx._core.PyNoAppError:\n    if wx.version() < '2.9':\n        tmpApp = wx.PySimpleApp()\n    else:\n        tmpApp = wx.App(False)\n\n# Get a dict of locale aliases from wx.Locale() -- same cross-platform (Win 7, Mac 10.9)\nlocale = wx.Locale()\naliases = {u'English (U.S.)': 'en_US'}\n# set defaults because locale.GetLanguageInfo(0) can return None on some systems:\nwxIdFromCode = {'en_US': wx.LANGUAGE_DEFAULT}  # int: 0 default, 2-229\ncodeFromWxId = {wx.LANGUAGE_DEFAULT: 'en_US'}  # used in directory names e.g. ja_JP; never JPN ala Windows\nfor i in range(230):\n    info = locale.GetLanguageInfo(i)\n    if info:\n        aliases[info.Description] = info.CanonicalName  # mix of forms: ja or ja_JP\n        wxIdFromCode[info.CanonicalName] = i\n        codeFromWxId[i] = info.CanonicalName\n\n# read all known mappings cross-platform from a file:\nwinmap = {'en_US': 'ENU'}  # get windows 3-letter code (=val) from canonical form (=key); use only for setting locale (non-wx)\nlocname = {'en_US': u'English (U.S.)'}  # descriptive name, if available; 5-letter code if not\nreverseMap = {u'English (U.S.)': 'en_US'}\nmappings = os.path.join(os.path.dirname(__file__), 'mappings.txt')\nfor line in codecs.open(mappings, 'rU', 'utf8').readlines():\n    try:\n        can, win, name = line.strip().split(' ', 2)  # canonical, windows, name-with-spaces\n    except ValueError:\n        can, win = line.strip().split(' ', 1)\n        name = can\n    winmap[can] = win\n    locname[can] = name\n    reverseMap[name] = can\n\n# what are the available translations? available languages on the OS?\nexpr = os.path.join(os.path.dirname(__file__), '..', 'locale', '*')\navailable = sorted(map(os.path.basename, glob.glob(expr)))\nsysAvail = [str(l) for l in codeFromWxId.values()  # installed language packs\n            if l and locale.IsAvailable(wxIdFromCode[l])]\n\ndef getID(lang=None):\n    \"\"\"Get wx ID of language to use for translations: `lang`, pref, or system default.\n\n    `lang` is a 5 char `language_REGION`, eg ja_JP\n    \"\"\"\n    if lang:\n        val = lang\n    else:\n        try:\n            val = prefs.app['locale']\n        except KeyError:\n            val = locale.GetLocale()  # wx.Locale, no encoding\n        if not val:\n            val = codeFromWxId[wx.LANGUAGE_DEFAULT]\n    try:\n        # out-dated: [can't set wx.Locale here because no app yet] now there is an app\n        # here just determine the value to be used when it can be set\n        language = wxIdFromCode[val]\n    except KeyError:\n        logging.error('locale %s not known to wx.Locale, using default' % val)\n        language = wx.LANGUAGE_DEFAULT\n\n    return language, val\n\nlanguageID, lang = getID()\n#use lang like this:\n#import locale  -- the non-wx version of locale\n#\n#if sys.platform.startswith('win'):\n#        v = winmap[val]\n#else: v=val\n#locale.setlocale(locale.LC_ALL, (v, 'UTF-8'))\n\n# set locale before splash screen:\nif locale.IsAvailable(languageID):\n    wxlocale = wx.Locale(languageID)\nelse:\n    wxlocale = wx.Locale(wx.LANGUAGE_DEFAULT)\n\n# ideally rewrite the following using wxlocale only:\npath = os.path.join(os.path.dirname(__file__), '..', 'locale', lang, 'LC_MESSAGE') + os.sep\nmofile = os.path.join(path, 'messages.mo')\ntry:\n    logging.debug(\"Opening message catalog %s for locale %s\" % (mofile, lang))\n    trans = gettext.GNUTranslations(open(mofile, \"rb\"))\nexcept IOError:\n    logging.debug(\"Locale for '%s' not found. Using default.\" % lang)\n    trans = gettext.NullTranslations()\ntrans.install(unicode=True)\n\n# to avoid a crash, PsychoPy app uses a nonstandard name _translate instead of _\n# seems like a var in a dependency is named _, clobbering _ as global translation:\n__builtins__['_translate'] = _\ndel(__builtins__['_'])  # idea: force psychopy code to use _translate\n\n\n#__builtins__['_'] = wx.GetTranslation\n# this seems to have no effect, needs more investigation:\n#path = os.path.join(os.path.dirname(__file__), '..', 'locale', lang, 'LC_MESSAGE') + os.sep\n#wxlocale.AddCatalogLookupPathPrefix(path)\n", "path": "psychopy/app/localization/__init__.py"}]}