1

I am a new Python user and I have been working through a number of tutorials. This has included running some of the code from the Command Prompt. This worked fine when I first tested the code but for some reason it seems to have stopped working and I am now getting errors when using Input(). I have included the code below and the error message I am receiving.

Code:

   import sys
print (sys.version)

print("hello world")
myName = input("What is your name?")
print(myName)

if (myName == "Matt"):
    print("Matt is great!")
elif (myName == "Bob"):
    print("Bob is ok")
else:
    print("Hello world")

input("Press enter to continue")

Error Message:

C:\Users\matt.xxxx>cd C:\Python34\Scripts\Projects

C:\Python34\Scripts\Projects>helloworld.py
2.7.7 (default, Jun  1 2014, 14:21:57) [MSC v.1500 64 bit (AMD64)]
hello world
What is your name?Matt
Traceback (most recent call last):
  File "C:\Python34\Scripts\Projects\helloworld.py", line 6, in <module>
    myName = input("What is your name?")
  File "<string>", line 1, in <module>
NameError: name 'Matt' is not defined

C:\Python34\Scripts\Projects>

I know this can occur when using older versions of python, however I have checked and I am fairly sure I am using version 3.4 (checked using import sys etc.). I have recently installed PyCharm which is the only thing I can think of that has changed. The code works in PyCharm and from IDLE but not from the Command Prompt. Any help would be much appreciated.

7
  • Can you update the question with the output of - import sys; print(sys.version) inside your script? Commented Jul 5, 2015 at 18:51
  • Hi Anand. Thanks for your quick response. When I run the code with print(sys.version) it does actually say 2.7.7??? However this isn't the case when I check the version when loading Python from the Command Prompt. Does this make sense. I had no idea I even had 2.7.7 on my machine! Do you know how to ensure 3.4 is used? Commented Jul 5, 2015 at 19:05
  • what is the exact command you are using to run the script? Commented Jul 5, 2015 at 19:16
  • And what is the result of echo %PATH% in your command prompt Commented Jul 5, 2015 at 19:21
  • Code and output have been updated Commented Jul 5, 2015 at 19:29

1 Answer 1

1

From your example , I believe you are running the script using - helloworld.py - this would cause Windows to lookup the default application associated with the extension .py and run it.

I am guessing in your case when you installed PyCharm, it somehow made Python 2.7.7 the default application for .py files (Or it was like that from the start) so when you directly run .py files (even from command prompt) they run using Python 2.7.7 .

You said in your comment that when running python directly from command prompt, you are getting python 3.4 , so the easiest way to fix your issue would be to use that to run your script.

Run it using the command -

python helloworld.py

As a long term solution, you may want to consider changing the default application associated with .py files. You can checkout this link for guide on how to do that

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.