{"action":"created","issue":{"url":"https://api.github.com/repos/enthought/pyface/issues/116","labels_url":"https://api.github.com/repos/enthought/pyface/issues/116/labels{/name}","comments_url":"https://api.github.com/repos/enthought/pyface/issues/116/comments","events_url":"https://api.github.com/repos/enthought/pyface/issues/116/events","html_url":"https://github.com/enthought/pyface/issues/116","id":54582970,"number":116,"title":"Problem locating icons for standalone applications generated by cx_freeze/py2exe","user":{"login":"pbrod","id":1193469,"avatar_url":"https://avatars.githubusercontent.com/u/1193469?v=3","gravatar_id":"","url":"https://api.github.com/users/pbrod","html_url":"https://github.com/pbrod","followers_url":"https://api.github.com/users/pbrod/followers","following_url":"https://api.github.com/users/pbrod/following{/other_user}","gists_url":"https://api.github.com/users/pbrod/gists{/gist_id}","starred_url":"https://api.github.com/users/pbrod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pbrod/subscriptions","organizations_url":"https://api.github.com/users/pbrod/orgs","repos_url":"https://api.github.com/users/pbrod/repos","events_url":"https://api.github.com/users/pbrod/events{/privacy}","received_events_url":"https://api.github.com/users/pbrod/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-01-16T14:48:54Z","updated_at":"2015-08-26T13:53:03Z","closed_at":"2015-08-26T13:53:02Z","body":"When running traits from a standalone executable, icons are expected to be found inside the executable itself. This is because of the statement \r\n\"dirname(getattr( sys.modules.get( module ), '__file__' ))\" which is used to get the path to the icon folder. The assumption is that the icons resides in subfolders of the module folder (i.e. 'pyface\\dock\\images', 'pyface\\images', 'pyface\\ui\\wx\\grid\\images' and 'pyface\\ui\\wx\\images').\r\nWhen generating a standalone executable, it is convenient to have the icons in a folder OUTSIDE the executable file. Thus, the function 'resource_path' in 'pyface\\resource\\resource_path.py' has to be modified. The code below is a proposal on how that could be done (see also enthought/traits#228):\r\n\r\n\tdef resource_path ( level = 2 ):\r\n\t    \"\"\"Returns a resource path calculated from the caller's stack.\r\n\t    \"\"\"\r\n\t    module = sys._getframe( level ).f_globals.get( '__name__', '__main__' )\r\n\t\r\n\t    if module != '__main__':\r\n\t        # Return the path to the module:\r\n\t        try:\r\n\t            path = dirname(getattr( sys.modules.get( module ), '__file__' ))\r\n\t        except:\r\n\t            # Apparently 'module' is not a registered module...treat it like\r\n\t            # '__main__':\r\n\t            pass\r\n\t        else:\r\n\t            if hasattr(sys, 'frozen'):\r\n\t                app_dir, app_name = os.path.split(sys.executable)\r\n\t                sub_dir = path.replace(sys.executable, '')\r\n\t                path = ''.join(app_dir, sub_dir)\r\n\t            return path\r\n\t\r\n\t    # '__main__' is not a real module, so we need a work around:\r\n\t    for path in [ dirname( sys.argv[0] ), getcwd() ]:\r\n\t        if exists( path ):\r\n\t            break\r\n\t\r\n\t    return path\r\n"},"comment":{"url":"https://api.github.com/repos/enthought/pyface/issues/comments/135027771","html_url":"https://github.com/enthought/pyface/issues/116#issuecomment-135027771","issue_url":"https://api.github.com/repos/enthought/pyface/issues/116","id":135027771,"user":{"login":"pbrod","id":1193469,"avatar_url":"https://avatars.githubusercontent.com/u/1193469?v=3","gravatar_id":"","url":"https://api.github.com/users/pbrod","html_url":"https://github.com/pbrod","followers_url":"https://api.github.com/users/pbrod/followers","following_url":"https://api.github.com/users/pbrod/following{/other_user}","gists_url":"https://api.github.com/users/pbrod/gists{/gist_id}","starred_url":"https://api.github.com/users/pbrod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pbrod/subscriptions","organizations_url":"https://api.github.com/users/pbrod/orgs","repos_url":"https://api.github.com/users/pbrod/repos","events_url":"https://api.github.com/users/pbrod/events{/privacy}","received_events_url":"https://api.github.com/users/pbrod/received_events","type":"User","site_admin":false},"created_at":"2015-08-26T13:53:02Z","updated_at":"2015-08-26T13:53:02Z","body":"Thank you; the suggested solution works like a charm!\r\n \r\nBelow is the resulting code adapted to our application. A workaround for “traitsui” images/icons is also included.\r\n \r\nNote that all images/icons in the “pyface” and “traitsui” packages are copied and placed in the application folder with folder structure preserved.\r\n \r\n    import sys\r\n    import os\r\n    from pyface.resource_manager import resource_manager\r\n \r\n   \r\n    def set_pyface_resource_path():\r\n        \"\"\"Set Pyface image/icon path to local application folder\"\"\"\r\n        if hasattr(sys, 'frozen'):  # If we're in an .exe file\r\n            path = os.path.dirname(sys.executable)\r\n            for root_path in ['pyface', 'traitsui']:\r\n                for root, dirs, _files in os.walk(root_path):\r\n                    if 'images' in dirs:\r\n                        resource_manager.extra_paths.append(os.path.join(path, root))\r\n \r\n\r\n    def set_traitsui_resource_path():\r\n        \"\"\"Set Traitsui image/icon path to local application folder\"\"\"\r\n        if hasattr(sys, 'frozen'):  # If we're in an .exe file\r\n            path = os.path.dirname(sys.executable)\r\n            os.environ['TRAITS_IMAGES'] = os.path.join(path, r'traitsui\\image\\library')\r\n "}}