import function from a file in the same folder

后端 未结 4 1185
甜味超标
甜味超标 2020-12-29 01:36

I\'m building a Flask app with Python 3.5 following a tutorial, based on different import rules. By looking for similar questions, I managed to solve an ImportError based on

4条回答
  •  滥情空心
    2020-12-29 02:29

    # imports all functions    
    import config
    # you invoke it this way
    config.my_function()
    

    or

    # import specific function
    from config import my_function
    # you invoke it this way
    my_function()
    

    If the app.py is invoked not from the same folder you can do this:

    # csfp - current_script_folder_path
    csfp = os.path.abspath(os.path.dirname(__file__))
    if csfp not in sys.path:
        sys.path.insert(0, csfp)
    # import it and invoke it by one of the ways described above
    

提交回复
热议问题