文本进度条实验为例:mac系统下‘终端‘执行python文件问题汇总

一、zsh: command not found

(base) liuyang@RyandeMacBook-Pro ~ % 文本进度条复习.py
zsh: command not found: 文本进度条复习.py

此时终端找不到文件需要手动添加文件路径,我一般把文件放在文稿(documents)目录下,输入documents/文本进度条复习.py系统就可以找到了,如下图所示:

(base) liuyang@RyandeMacBook-Pro ~ % Documents/文本进度条复习.py

二、zsh: permission denied
此时没有对文件的执行权限,使用chmod a+x ./documents/文件名获取执行权限。
参考链接https://blog.csdn.net/skillart/article/details/38443753

(base) liuyang@RyandeMacBook-Pro ~ % chmod a+x ./documents/文本进度条复习.py

三、import: command not found

(base) liuyang@RyandeMacBook-Pro ~ % documents/文本进度条复习.py
documents/文本进度条复习.py: line 2: import: command not found
documents/文本进度条复习.py: line 3: scale: command not found
documents/文本进度条复习.py: line 4: syntax error near unexpected token `'--------- Progress Start---------''
documents/文本进度条复习.py: line 4: `print('--------- Progress Start---------')'

此时终端可以执行文件但是无法调用python标准库、无法按照python程序执行文件,需要在python源代码的第一行添加#!/usr/bin/python(该语句默认执行python2)语句,如下图所示:

#!/usr/bin/python
#TextProgress Bar.py
import time
for i in range(101):
        print('\r{:3}%'.format(i),end="")
        time.sleep(0.5)

四、SyntaxError: invalid syntax

(base) liuyang@RyandeMacBook-Pro ~ % documents/文本进度条复习.py
documents/文本进度条复习.py: line 2: import: command not found
documents/文本进度条复习.py: line 3: syntax error near unexpected token `('
documents/文本进度条复习.py: line 3: `for i in range(101):'
(base) liuyang@RyandeMacBook-Pro ~ % documents/文本进度条复习.py
  File "documents/文本进度条复习.py", line 5
    print('\r{:3}%'.format(i),end="")
                                 ^
SyntaxError: invalid syntax

这段python源代码在idle下执行正常,但是在终端下执行 end = ‘’(不换行指令)出现报错。可能是默认执行python2出现了错误。此时需要在代码首句关联python3,修改首行代码如下图所示:

#!/usr/bin/python3
#TextProgress Bar.py
import time
for i in range(101):
        print('\r{:3}%'.format(i),end="")
        time.sleep(0.5)

最终执行就可以在‘终端’执行python程序并实时进行文本进度条刷新啦!

(base) liuyang@RyandeMacBook-Pro ~ % documents/文本进度条复习.py
 12%
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页