Python Name Error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python Name Error
# 1  
Old 01-30-2015
Python Name Error

Hi There,

I have the following code:

Code:
import time
import paramiko


node_list = ['oj_msp_ts1', 'oj_msp_ts2', 'oj_msp_ts3', 'oj_msp_ts4', 'oj_msp_ts5', 'oj_msp_ts6', 'oj_msp_ts7', 'oj_msp_ts8', 'oj_msp_ts9', 'oj_msp_ts10', 'oj_msp_ts11', 'oj_msp_ts12', 'oj_msp_ts13']
for node in node_list:
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(node, username='lord', password='xxxx')
    variables = {}
    stdin, stdout, stderr = ssh.exec_command("snmpwalk -v1 localhost -c public .1.3.6.1.4.1.193.126.3.6.1.1.1.1.7.5 | awk '{print $NF}'")
    variables[node + "_pull_first"] = stdout.read().strip()
    print str(oj_msp_ts1_pull_first)
    ssh.close()

But for some reason which I have been thinking about for 2 days now, it keeps giving the following error:

NameError: name 'oj_msp_ts1_pull_first' is not defined

This is quite funny to figure out and am somehow surprised that am having issue with (it should be easy to figure out, it's just a couple of lines).

Any input will be appreciated.
# 2  
Old 01-30-2015
If you showed me where it is defined, I'd share your surprise.
# 3  
Old 01-30-2015
Hi RudiC,

This is where it is defined via the dict:

Code:
variables[node + "_pull_first"] = stdout.read().strip()

node is been picked up by the FOR loop (node_list).

I hope it is clear
# 4  
Old 01-30-2015
So the name isn't blahblah_pull_first, it's variables['blahblah_pullfirst']

Change it to that.... you'll probably want an intervening var with the dynamic name:

dynname = node + "_pull_first"

so you can use that name in both places (variables[dynname])
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Programming

Tuple Error in Python

Hi, I am getting the below error: AttributeError: 'tuple' object has no attribute 'field_id' This line is complaining on the below code snippet: field_list = for f in source_rows: field_list.append(f.field_id) Where source_rows returns multiple rows of... (1 Reply)
Discussion started by: ChicagoBlues
1 Replies

3. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Shell Programming and Scripting

python - string encoding error

I'm trying to pull a google calendar (successful) and then put the contents into a mysql db (almost successful). On one of the field I keep getting an encode error: #!/usr/bin/python from xml.etree import ElementTree import gdata.calendar.data import gdata.calendar.client import... (12 Replies)
Discussion started by: unclecameron
12 Replies

5. Shell Programming and Scripting

python script error

Hi I have written a script in python and used it inside Ubuntu and its worked for me. The script import email.utils, however in centos when I try to run the script I get: Traceback (most recent call last): File "./warning.py", line 4, in ? import email.utils ImportError: No... (2 Replies)
Discussion started by: programAngel
2 Replies

6. Programming

Python MySQL error

Hey all, I'm trying to enter some data into a database and I keep getting this error: Traceback (most recent call last): File "./ais_file_parser.py", line 77, in <module> cursor.execute(sql) File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 166, in execute ... (1 Reply)
Discussion started by: pmd006
1 Replies

7. Solaris

Error in installing the mysql-python-1.2.3.tar.gz

Hi, I followed the step written in README. It got some error message when I type " #python setup.py build" running build running build_py copying MySQLdb/release.py -> build/lib.solaris-2.11-i86pc-2.4/MySQLdb running build_ext building '_mysql' extension /usr/lib/python2.4/pycc -DNDEBUG... (2 Replies)
Discussion started by: AlexCheung
2 Replies

8. Programming

Error in Python Embeded in Qt C++

hi i have an error in script language when run it i use python 2.4, CentOS 5.5, Qt C++ 2010/04 the error is SystemError: null argument to internal routine the Python Code def main(i): print i if __name__ == "__main__": main(i) the C++ Code PyObject *objModule =... (0 Replies)
Discussion started by: HanyM.Magdy
0 Replies

9. Shell Programming and Scripting

use python or awk to match names 'with error tolerance'

I think this is a very challenging problem I am facing and I have no idea how to deal with it Suppose I have two csv files A.csv Toyota Camry,1998,blue Honda Civic,1999,blue B.csv Toyota Inc. Camry, 2000km Honda Corp Civic,1500km I want to generate C.csv Toyota Camry,1998,blue... (7 Replies)
Discussion started by: grossgermany
7 Replies
Login or Register to Ask a Question