Import Python Script Into Another?

后端 未结 5 1619
花落未央
花落未央 2020-11-28 23:59

I\'m going through Zed Shaw\'s Learn Python The Hard Way and I\'m on lesson 26. In this lesson we have to fix some code, and the code calls functions from another script. He

5条回答
  •  暖寄归人
    2020-11-29 00:59

    Following worked for me and it seems very simple as well:

    Let's assume that we want to import a script ./data/get_my_file.py and want to access get_set1() function in it.

    import sys
    sys.path.insert(0, './data/')
    import get_my_file as db
    
    print (db.get_set1())
    

提交回复
热议问题