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.
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
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.
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
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.
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
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.
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