Python 2.7 NameError : name 'hello' (my file) name is not defined

Question:

I am not sure if this question has been answered, but before you say yes please look at my problem more carefully. I am a beginner in python, though I have set my EVN and everything works good there. To make some advance stuffs, I need to create my file and I named it: hello.py
From command line I can navigate to the directory and then I tried to execute the hello.py. It contains the following code.

import sys

class Greeting:

    def main(argv):
        # My code here
        print ("Good day")

if __name__ == "__main__":
    Greeting().main(sys.argv)

Here is the error I am getting

>>> hello.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined
>>>

Could it be that file “hello.py” needs to be define some where, I don’t really understand.

Asked By: Ityav

||

Answers:

To run python files you must use python hello.py

To make the file executable on Linux or Mac you can also add

#!/bin/python

to the top of the file to run with just hello.py

Answered By: user9478968

You should run a file using python {file_name}.py.

import sys

class Greeting:

    def main(argv):
        # My code here
        print ("Good day")

if __name__ == "__main__":
    Greeting().main(sys.argv)

if your saved file name is hello.py
then use python hello.py in terminal.

Answered By: atiq1589
flag = True
print('hello I am Academia. Start typing your query after greeting to me. for Ending convo type bye')
while(flag == True):
 user_response = input()
 user_response = user_response.lower()
 if('user_response!= bye'):
   if(user_response == 'thaknyou' or user_response == 'thanks'):
     flag= False
     print('Academia: your welcome..')
   else:
     if(greet(user_response)!=None):
          print('Academia: '+ greet(user_response))
     else:
            sentence_tokens.append(user_response)
            word_tokens = word_tokens + nltk.word_tokenize(user_response)
            final_words =list(set(word_tokens))
            print('Academia: ', end = '')
            print(response(user_response))
            sentence_tokens.remove(user_response)
 else:
               flag = False 
               print('Academia: goodbye')

name error showing
name greeting is not defined

Answered By: Priya Singh
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.