在vscode里能运行的python代码在mac终端机运行出现了zsh:command not found

MACBOOK Pro M1,在vscode里面试了一下python是能够运行的。

img


img

但是在终端机里面运行的时候就出现了 zsh:command not found

img

看了一圈然后创建了.zshrc文件和.bash_profile文件, 再把下面三行脚本加了进去。

img

现在变长能看到python版本(本来是看不到的),s输入python也能直接运行python。

img


img

但运行helloworld.py的时候就是出现zsh:command not found,怎么解决啊?

  1. 运行时要加上解释器:python hello_world.py
  2. 或者在文件内添加首行 #!/usr/bin/env python,然后终端运行 chmod +x hello_world.py 给程序加上执行权限,然后在终端内运行 ./hello_world.py。注意一定要有这个 ./

示例:

#!/usr/bin/env python
print('-----')
print('hello world')
print('-----')