python将变量从另一个脚本调用到当前脚本中

时间:2016-10-15 08:25:20

标签: python function import

我试图将另一个脚本中的变量调用到我当前的脚本中,但是遇到了未定义的变量"的问题。

botoGetTags.py

20 def findLargestIP():
 21         for i in tagList:
 22                 #remove all the spacing in the tags
 23                 ec2Tags = i.strip()
 24                 #seperate any multiple tags
 25                 ec2SingleTag = ec2Tags.split(',')
 26                 #find the last octect of the ip address
 27                 fullIPTag = ec2SingleTag[1].split('.')
 28                 #remove the CIDR from ip to get the last octect
 29                 lastIPsTag = fullIPTag[3].split('/')
 30                 lastOctect = lastIPsTag[0]
 31                 ipList.append(lastOctect)
 32                 largestIP  = int(ipList[0])
 33                 for latestIP in ipList:
 34                         if int(latestIP) > largestIP:
 35                                 largestIP = latestIP
 36         return largestIP
 37         #print largestIP
 38
 39 if __name__ == '__main__':
 40         getec2Tags()
 41         largestIP = findLargestIP()
 42         print largestIP

因此,此脚本^正确返回largestIP的值,但在我的其他脚本中

terraform.py

  1 import botoGetTags
  8 largestIP = findLargestIP()

在我执行脚本terraTFgen.py中的任何函数之前,我得到:

Traceback (most recent call last):
  File "terraTFgen.py", line 8, in <module>
    largestIP = findLargestIP()
NameError: name 'findLargestIP' is not defined

我认为如果我导入另一个脚本,我可以在当前脚本中使用这些变量,那么我应该采取另一个步骤吗?

由于

1 个答案:

答案 0 :(得分:2)

您导入了模块,而不是函数。所以你需要通过模块来参考这个功能:

import botoGetTags
largestIP = botoGetTags.findLargestIP()

或者您可以直接导入该功能:

from botoGetTags import findLargestIP
largestIP = findLargestIP()
相关问题