File: pyedit-products/unzipped/PP4E/Gui/Tools/windows-notes.txt

general (and cryptic) notes on modules accessing its own folder, regardless
of usage modes (imported fron package or run standalone);  in short, can 
always grab the folder path from __file__ without importing self;

==================================================================================
in TextEditor

importlib.import_module would avoid loop, but import not required at all

get icons from module's own directory, whether imported or run
(don't copy to pymailgui's (and all other clients): needs own)

    import importlib
    
    # my name: imported from package or run
    print('NAME\t', repr(__name__))

    # mod = first in package path, not last
    mymod = __import__(__name__)
    print('MODDIR1\t', repr(os.path.dirname(mymod.__file__)))

    # last in package path, not first
    mymod = importlib.import_module(__name__)  
    print('MODDIR2\t', repr(os.path.dirname(mymod.__file__)))
    
    # but no need to import at all to get a complete path __file__
    print('FILE\t', repr(__file__), '\nDIR\t', repr(os.path.dirname(__file__)))
    mydir = os.path.dirname(__file__)

    """
    when imported:
    NAME     'PP4E.Gui.TextEditor.textEditor'
    MODDIR1  'c:\\MY-STUFF\\Code\\pymailgui\\Live\\PyMailGui-PP4E\\PP4E'
    MODDIR2  'c:\\MY-STUFF\\Code\\pymailgui\\Live\\PyMailGui-PP4E\\PP4E\\Gui\\TextEditor'
    FILE     'c:\\MY-STUFF\\Code\\pymailgui\\Live\\PyMailGui-PP4E\\PP4E\\Gui\\TextEditor\\textEditor.py'
    DIR      'c:\\MY-STUFF\\Code\\pymailgui\\Live\\PyMailGui-PP4E\\PP4E\\Gui\\TextEditor'

    when run:
    '__main__'
    MODDIR1  ''
    MODDIR2  ''
    FILE     'textEditor.py'
    DIR      ''
    """

    mydir = os.path.dirname(__file__)   # works whether imported from package or run
    try:
        if sys.platform.startswith('win'):
            # Windows, all contexts
            iconfilename = os.path.join(mydir, 'icons', 'pyedit.ico')
            window.iconbitmap(iconfilename)                
        elif sys.platform.startswith('linux'):
            # Linux app bar, Tk 8.5+
            iconfilename = os.path.join(mydir, 'icons', 'pyedit.gif')
            ...

==========================================================================================
in PP4E/Gui/Tools/windows.py

same story: importlib.import_module would avoid loop, but import not required at all

        else:                                              # try tools dir icon
            """
            mymod  = __import__(__name__)                  # import self for dir
            path   = __name__.split('.')                   # poss a package path
            for mod in path[1:]:                           # follow path to end
                mymod = getattr(mymod, mod)                # only have leftmost
            mydir  = os.path.dirname(mymod.__file__)
            """
            mydir  = os.path.dirname(__file__)             # import not needed!
            myicon = os.path.join(mydir, self.iconmine)    # use myicon, not tk

==========================================================================================



[Home page] Books Code Blog Python Author Train Find ©M.Lutz