[英]python calling variables from another script into current script
我正在嘗試從另一個腳本中將變量調用到當前腳本中,但遇到“未定義變量”問題。
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
我以為如果導入另一個腳本,則可以在當前腳本中使用這些變量,是否應該采取其他步驟?
謝謝
您導入的是模塊,而不是功能。 因此,您需要通過模塊引用該函數:
import botoGetTags
largestIP = botoGetTags.findLargestIP()
或者,您可以直接導入函數:
from botoGetTags import findLargestIP
largestIP = findLargestIP()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.