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

2024-09-28 03:19:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将另一个脚本中的变量调用到当前脚本中,但遇到了“变量未定义”问题。你知道吗

你知道吗博托吉塔.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的值,但是在我的另一个脚本中

你知道吗地形.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

我想,如果我导入另一个脚本,我可以在我当前的脚本中使用这些变量,我还应该采取其他步骤吗?你知道吗

谢谢


Tags: theinpy脚本fortagsremovesplit
1条回答
网友
1楼 · 发布于 2024-09-28 03:19:03

您导入的是模块,而不是函数。因此,您需要通过模块参考函数:

import botoGetTags
largestIP = botoGetTags.findLargestIP()

或者,您可以直接导入函数:

from botoGetTags import findLargestIP
largestIP = findLargestIP()

相关问题 更多 >

    热门问题