Skip to content Skip to sidebar Skip to footer

Nameerror: Name 'host' Is Not Defined

I'm new in python programming. When i try running a simple python script i get error like this in my terminal root@bt:/tmp# python code.py Traceback (most recent call last): File '

Solution 1:

Your except clause will catch any error in any line of code in the try block. If you don't specify enough arguments on the command line, the line host = str(sys.argv[1]) will fail, leaving host unassigned, which then causes the error you are seeing when you try to print it.

You should take most of the code out of your try block, really, and/or create multiple try blocks that catch errors in much smaller chunks of code. Furthermore, you should specify the actual exception type you want to handle with each except instead of trying to handle all of them. Bare except: catches things you probably don't want caught, such as KeyboardInterrupt and SystemExit. If you must catch most exceptions, use except Exception: instead of just except:.

Solution 2:

it seem that your script expects an input parameter

host=str(sys.argv[1])

in case that parameter is not supplied, as shown in your post, an exception raised and been caught in the except clause before the host parameter was defined

try to declare host before the try/except block

Solution 3:

you are defining host in the first line of try/except i believe the error is in that first line. to debug this take remove the try/except to see what the actual error is.

Post a Comment for "Nameerror: Name 'host' Is Not Defined"