1

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.

Sixx
  • 11
  • 4
  • Copy-and-paste the code and error **as text** into your question; content only given in a screenshot isn't "included in the question itself" as required for [mre] rules. (See the [meta.se] question [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/a/285557/14122)) – Charles Duffy Jan 14 '21 at 23:43
  • 2
    I'm 99% certain that you are running Python 3 on your Windows PC and Python 2 on your Macbook (which is the default on macOS) – UnholySheep Jan 14 '21 at 23:43

2 Answers2

2

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.

DapperDuck
  • 2,728
  • 1
  • 9
  • 21
  • 1
    Also note that there are stable and up to date Python 3 releases for Mac - it is highly recommended to upgrade to Python 3, unless you really have no option to do so. – Grismar Jan 14 '21 at 23:49
  • I agree, this is important to note. I will add it to my answer, in addition to details how to use different interpreters with Visual Studio Code. – DapperDuck Jan 14 '21 at 23:50
  • If this is the OP's problem (which I can't tell, with the critical information being only in a screenshot), the question should be closed as duplicate, not answered. In [How to Answer](https://stackoverflow.com/help/how-to-answer), see the *Answer Well-Asked Questions* section, and therein the bullet point regarding questions that "have been asked and answered many times before". – Charles Duffy Jan 14 '21 at 23:52
  • I agree, however the OP also mentioned concerns with the shift to M1 chips and the way python was behaving. I am planning to add information on how to add Python 3.9 to Macs with M1 chips, and information about how to use different interpreters with vscode – DapperDuck Jan 14 '21 at 23:56
  • @Sixx Updated to include information about how to install and us vscode with Python 3.9.1 – DapperDuck Jan 15 '21 at 00:14
0

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.

Sixx
  • 11
  • 4