我在全局脚本中将policydict定义为policydict = {},当我从主脚本运行更新策略时,得到的回溯policydict未定义。
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()
回溯(最近通话最近): 文件“ C:/Users/n0256468/PycharmProjects/scorecard/Main.py”,第16行,在 updatepolicies() 文件“ C:\ Users \ n0256468 \ PycharmProjects \ scorecard \ policies.py”,第38行,位于updatepolicies中 policydict [row ['政策名称']] =返回值[2] NameError:名称“ policydict”未定义
以退出代码1完成的过程
答案 0 :(得分:1)
似乎您在另一个文件中定义了policydict
,但是您并未在代码中的任何地方使用from module import policydict
。如果仅导入模块,则必须在变量前加上模块名称。例如,如果policydict
在mymodule
中:
import mymodule
print(mymodule.policydict)
如果不想使用模块名称作为前缀,则可以执行以下操作:
from mymodule import policydict
print(policydict)
写入global policydict
是指当前文件中的全局变量policydict
。