Reputation: 11
I've wrote a simple code assigning an inputed value to a variable and print such variable, the same code works on my windows pc but not on my M1 MacBook Air. Is this an issue on my end where I am missing something out perhaps or is this just an issue with the M1 chip? I'm fairly new to coding so maybe I have some wrong settings in vs code, any help would be appreciated. screenshot from my MacBook
python -u "/Users/jeff/Documents/whynowork.py"
jeff@Jeffs-MacBook-Air ~ % python -u
"/Users/jeff/Documents/whynowork.py"
Enter input:hello
Traceback (most recent call last):
File "/Users/jeff/Documents/whynowork.py", line 1, in <module>
example = input("Enter input:")
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
jeff@Jeffs-MacBook-Air ~ %
I have gotten replies that I am using python 2, but in my vs code at the bottom (even in the screenshot you can see that) I think I am using python 3.8.2.
Upvotes: 1
Views: 2612
Reputation: 11
I have deduced from the feedback that MacOS's default python is python 2, this overrides any paths to python in vs code as in my screenshot it appeared to be in version 3.8.2. This video https://www.youtube.com/watch?v=K5jk-sNgeSY outlines how to get python 3 on MacOS, although it says it's for Catalina it also works for Big Sur.
Upvotes: 0
Reputation: 2859
This is because you are running Python 2 on your Mac device. The error states that name 'hello' is not defined
. This error occurs when using input()
rather than raw_input
in Python 2, see here. This is the code converted to Python 2.7:
example = raw_input("Enter Input:")
print(example)
It is recommended to upgrade to Python 3, for more up-to-date features and libraries. The latest Python 3 release is 3.9.1, which supports Mac M1 Chips. Here is a tutorial on how to install.
To change your python interpreter in Visual Studio Code, go to
View >> Command Palette >> Search for Python: Select Interpreter >>
Select Python 3.9.1, if it doesn't appear click Enter interpreter path and browse to find your python installation and click the python file.
Upvotes: 2