Scripting inside a Python application

前端 未结 4 1907
清歌不尽
清歌不尽 2021-01-19 00:50


I\'d like to include Python scripting in one of my applications, that is written in Python itself.

My application must be able to call extern

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-19 01:15

    Use __import__ to import the files provided by the user. This function will return a module. Use that to call the functions from the imported file.

    Use try..except both on __import__ and on the actual call to catch errors.

    Example:

    m = None
    try:
        m = __import__("external_module")
    except:
        # invalid module - show error
    if m:
        try:
            m.user_defined_func()
        except:
            # some error - display it
    

提交回复
热议问题