chowpay
chowpay

Reputation: 1687

python calling variables from another script into current script

I'm trying to call a variable from another script into my current script but running into "variable not defined" issues.

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

So this script ^ correctly returns the value of largestIP but in my other script

terraform.py

  1 import botoGetTags
  8 largestIP = findLargestIP()

before I execute any of the functions in my script, terraTFgen.py, I get:

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

I thought that if I import another script I could use those variables in my current script is there another step I should take?

Thanks

Upvotes: 0

Views: 233

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599510

You imported the module, not the function. So you need to refer to the function via the module:

import botoGetTags
largestIP = botoGetTags.findLargestIP()

Alternatively you could import the function directly:

from botoGetTags import findLargestIP
largestIP = findLargestIP()

Upvotes: 2

Related Questions