Python从另一个文件导入变量

在写Python脚本时,我们可能希望从另一个脚本里面导入变量,分享下方法

文件A名为test1.py,变量名为localhost_hostname

localhost_hostname = run_cmd("cat /etc/host_name")[1]

文件B名为test2.py,需要使用到文件A名为test1.py文件里面的localhost_name变量

from test1 import localhost_hostname
print(localhost_hostname)

 

posted @ 2021-07-13 10:41  lucky_tomato  阅读(2177)  评论(2编辑  收藏  举报