由于 macOS 会保留 2.7 版本的 Python ,所以默认通过命令行运行「python」进入的是 2.7 版本
MBP ~ % python
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Jun 5 2020, 22:59:21)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> name = input('please enter your name: ')
please enter your name: jimmy
Traceback (most recent call last):
File "", line 1, in
File "", line 1, in
NameError: name 'jimmy' is not defined
>>>
按照这个提示,安装 3 以上版本通过 python3 命令进入就不会有问题
MBP ~ % python3
Python 3.8.6 (default, Oct 8 2020, 14:06:32)
[Clang 12.0.0 (clang-1200.0.32.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> name = input('please enter your name: ')
please enter your name: jimmy
>>> print('hello,', name)
hello, jimmy
>>>