run as administrator problem with py2exe
Question: I am having this problem that we compiled our python project into an exe with
py2exe and the resulting exe does not work unless it is executed as an
administrator.

I wanted to ask that is this supposed to happen ?? As in there are so many
applications that we can run as not an administrator so is there any way I can
convert my python code to such an application ...

Thanks a lot..


Answer: It sounds like your application is attempting to write to a directory for
which the basic user doesn't have access; most likely the "Program Files"
directory. I believe in Vista/Win7 this is not allowed, and the standard
convention is to write to the user's appdata folder, for any user data you
might wish to store.

You can reliably obtain the location of this directory using the `ctypes`
module, here's an example:

    
    
    import ctypes
    from ctypes import wintypes
    
    def get_appdata_directory():
        CSIDL_APPDATA = 0x001a
    
        dll = ctypes.windll.shell32
        app_data_directory = ctypes.create_unicode_buffer(wintypes.MAX_PATH)
    
        found = dll.SHGetFolderPathW(0, CSIDL_APPDATA, 0, 0, app_data_directory)
    
        # FYI: if `found` is False, then it failed to locate the appdata directory
        # and app_data_directory.value is empty. So you might want to add some
        # code here to verify that a valid path is going to be returned.
    
        # This would probably only happen on older versions of windows, 
        # but, this is just a guess as I don't have any older OSs available
        # for testing. (see my note below)
    
        return app_data_directory.value
    
    appdata = get_appdata_directory()
    print(appdata)
    # outputs something such as: 'C:\Users\bob\AppData'
    

Note: I believe the `appdata` folder was introduced with WinXP/Win2k. Not sure
about WinME and prior, however, I don't believe you have to worry about the
administrator restrictions on these earlier OSs. If you really care to support
them, you could use python's builtin `platform` module along with some
conditionals, then simply write user data to the "Program Files" directory for
the archaic versions of Windows.

