Issue with the global command within a function
<p>I am having an issue with getting this code to run properly:</p>

<pre><code>def ip_is_valid():
    check = False
    #Global exposes outside the local function
    global ip_list

    while True:
        #Prompting user for input
        print "\n" + "# " * 20 + "\n"
        ip_file = raw_input("# Enter IP file name followed by extension: ")
        print "\n" + "# " * 20 + "\n"

        #Changing exception message
        try:
            selected_ip_file = open(ip_file, 'r')
            #Start from the beginning of the file
            selected_ip_file.seek(0)

            ip_list = selected_ip_file.readlines()

            selected_ip_file.close()

        except IOError:
            print "\n* File %s does not exist. Please check and try again\n" % ip_file


        for ip in ip_list:
            a = ip.split('.')
            if (len(a) == 4) and (1 &lt;= int(a[0]) &lt;= 223) and (int(a[0]) != 127) and (int(a[0]) != 169 or int(a[1]) != 254) and (0 &lt;= int(a[1]) &lt;= 255 and 0 &lt;= int(a[2]) &lt;= 255 and 0 &lt;= int(a[3]) &lt;= 255):
                check = True
                break
            elif (len(a) == 4) and (224 &lt;= int(a[0]) &lt;= 239):
                print "\nThis is a multicast address. Please enter a unicast valid address\n"
                check = False
                continue
            elif (len(a) == 4) and (int(a[0]) == 127):
                print "\nThis is a loopback address and is not valid. Please try again.\n"
                check = False
                continue
            elif (len(a) == 4) and (int(a[0]) == 169 or int(a[1]) == 254):
                print "\n This is an APIPA address and is invalid. Please try again.\n"
                check = False
                continue
            else:
                print "\n* There was an invalid IP address. Please check and try again.\n"
                check = False
                continue

        if check == False:
            continue                
        elif check == True:
            break

ip_is_valid()
</code></pre>

<p>The issue I have is python will prompt for an IP file but follows with this error:</p>

<pre><code>  File ".\validip.py", line 133, in &lt;module&gt;
    ip_is_valid()
  File ".\validip.py", line 41, in ip_is_valid
    for ip in ip_list:
NameError: global name 'ip_list' is not defined
</code></pre>

<p>Even though I defined ip_list in the function, I am still getting that error. I am using "global" because there are other functions in this program that need visibility to the IP list variable.</p>

<pre><code>def create_threads():
    threads = []
    for ip in ip_list:
        th = threading.Thread(target = open_ssh_conn, args = (ip,)) 
        th.start()
        threads.append(th)
    for th in threads:
        th.join()

create_threads()
</code></pre>

<p>Before using variable <code>ip_list</code> as <code>global</code> variable, you must have to define it at the outer scope. For example in your case, you may make it run like: </p>

<pre><code>ip_list = []
def ip_is_valid():
    # Some logic
    global ip_list
    # Some more logic
</code></pre>

<p>OR define it based on where you need <code>ip_list</code>.</p>

<p><strong>PS:</strong> You have to define <code>ip_list</code> before you make call to <code>ip_is_valid</code> function</p>

python networking