jczic / MicroWebSrv2

The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
https://github.com/jczic/MicroWebSrv2
MIT License
662 stars 97 forks source link

MicroWebSrv2Exception: Cannot load module "PyhtmlTemplate" #5

Closed elSayaka closed 5 years ago

elSayaka commented 5 years ago

Hello. I found if the MicroWebSrv2 folder in another place, the system can't found the module in the mods folder. When I use MicroWebSrv2.LoadModule('PyhtmlTemplate') The traceback is src.MicroWebSrv2.microWebSrv2.MicroWebSrv2Exception: Cannot load module "PyhtmlTemplate".


Change the code in microWebSrc2.py can fix this error in my case. But this way is not good.

@staticmethod
    def LoadModule(modName) :
        if not isinstance(modName, str) or len(modName) == 0 :
            raise ValueError('"modName" must be a not empty string.')
        if modName in MicroWebSrv2._modules :
            raise MicroWebSrv2Exception('Module "%s" is already loaded.' % modName)
        try :
            # old
            # module   = __import__('MicroWebSrv2.mods.%s' % modName, { }, { }, [modName])
            # new
            module = __import__('src.MicroWebSrv2.mods.%s' % modName, {}, {}, [modName])
            modClass = getattr(module, modName)
            if type(modClass) is not type :
                raise Exception
            modInstance = modClass()
            MicroWebSrv2._modules[modName] = modInstance
            return modInstance
        except :
            raise MicroWebSrv2Exception('Cannot load module "%s".' % modName)
jczic commented 5 years ago

Ok I see, I'll work on the folder to automatically found it :)

elSayaka commented 5 years ago

Thank You very much :)

jczic commented 5 years ago

I've pushed this : https://github.com/jczic/MicroWebSrv2/commit/fc6a6dfc5c383bcf7320f74d7430903ac9b4db6a Do you can to try it please? Thank you very much :)

elSayaka commented 5 years ago

Nice! I tried it, It works success. Thank you very much :)

jczic commented 5 years ago

👍