-2

I defined policydict in a global script as policydict = {} and when i run update policies from my main script i get the traceback policydict not define.

import mysql.connector
import datetime
import xlrd
import re
import os
import csv

from Global import guardpointdict


def updatepolicies():
    global policydict

#read in csv from vormetric
    with open('policies.csv') as polcsvfile:
        policies = csv.DictReader(polcsvfile)
        for row in policies:
        #print(row['Report Id'], row['Status Id'], row['Host Name'], row["OS Type"], row['Host Description'], row['License Type'],
              #row['One-Way Enabled'], row['FS Agent Registration Status'], row['FS Agent Version'], row['Host Name'], row['Last Policy Update'],
              #row['Guard Point Id'], row['Guard Path'], row['Policy Id'], row['Policy Name'], row['Guard Enabled'],
              #row['Guard Point Status'], row['Error Reason'])



        #create database connection

            conn = mysql.connector.connect(host='xxx', user='xxx', 
            password='xxx', db='mydb')
            cursor = conn.cursor()


        #Create parameters for stored procedure
            policyparams = (row['Policy Name'], row['Policy Description'], 0)

        #call stored procedure
            returnvalue = cursor.callproc('update_policies', policyparams)

        # create dictionary used for keypair in guardpoints table
             policydict[row['Policy Name']] = returnvalue[2]



            conn.commit()
            conn.close()

Traceback (most recent call last): File "C:/Users/n0256468/PycharmProjects/scorecard/Main.py", line 16, in updatepolicies() File "C:\Users\n0256468\PycharmProjects\scorecard\policies.py", line 38, in updatepolicies policydict[row['Policy Name']] = returnvalue[2] NameError: name 'policydict' is not defined

Process finished with exit code 1

springcj1
  • 117
  • 1
  • 2
  • 13

1 Answers1

1

It appears that you defined policydict in another file, however you didn't use from module import policydict anywhere in your code. If you simply import the module, you have to prefix the variable with the module name. For example, if policydict was in mymodule:

import mymodule

print(mymodule.policydict)

If you don't want to prefix it with the module name, you could do this:

from mymodule import policydict

print(policydict)

Writing global policydict refers to the global variable policydict within the current file.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • Thank you David the Third that is exactly what i was missing. The import statement. From modulename import policydict. – springcj1 Dec 27 '18 at 16:43