0

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

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

awesome that works thanks. Which method is considered better etiquette?
There's not really a hard-and-fast rule, and PEP8 doesn't make any recommendations. It depends how readable each is in the particular circumstances. Some places though have a house style - at Google for example you must always import the module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.