How to fix NameError: name 'policydict' is not defined [duplicate]
This question already has an answer here:
Use of “global” keyword in Python
10 answers
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:Usersn0256468PycharmProjectsscorecardpolicies.py", line 38, in updatepolicies
policydict[row['Policy Name']] = returnvalue[2]
NameError: name 'policydict' is not defined
Process finished with exit code 1
python python-3.x
marked as duplicate by Jared Smith, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 27 '18 at 18:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Use of “global” keyword in Python
10 answers
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:Usersn0256468PycharmProjectsscorecardpolicies.py", line 38, in updatepolicies
policydict[row['Policy Name']] = returnvalue[2]
NameError: name 'policydict' is not defined
Process finished with exit code 1
python python-3.x
marked as duplicate by Jared Smith, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 27 '18 at 18:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
That's not howglobalworks.
– Jared Smith
Dec 27 '18 at 16:32
add a comment |
This question already has an answer here:
Use of “global” keyword in Python
10 answers
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:Usersn0256468PycharmProjectsscorecardpolicies.py", line 38, in updatepolicies
policydict[row['Policy Name']] = returnvalue[2]
NameError: name 'policydict' is not defined
Process finished with exit code 1
python python-3.x
This question already has an answer here:
Use of “global” keyword in Python
10 answers
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:Usersn0256468PycharmProjectsscorecardpolicies.py", line 38, in updatepolicies
policydict[row['Policy Name']] = returnvalue[2]
NameError: name 'policydict' is not defined
Process finished with exit code 1
This question already has an answer here:
Use of “global” keyword in Python
10 answers
python python-3.x
python python-3.x
asked Dec 27 '18 at 16:24
springcj1
325
325
marked as duplicate by Jared Smith, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 27 '18 at 18:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jared Smith, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 27 '18 at 18:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
That's not howglobalworks.
– Jared Smith
Dec 27 '18 at 16:32
add a comment |
1
That's not howglobalworks.
– Jared Smith
Dec 27 '18 at 16:32
1
1
That's not how
global works.– Jared Smith
Dec 27 '18 at 16:32
That's not how
global works.– Jared Smith
Dec 27 '18 at 16:32
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
add a comment |
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.
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
add a comment |
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.
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.
answered Dec 27 '18 at 16:34
David the third
1,3621723
1,3621723
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
add a comment |
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
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
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
add a comment |
1
That's not how
globalworks.– Jared Smith
Dec 27 '18 at 16:32