问题:
我一直可以从终端python运行(运行python解释器)脚本,但今天突然无法从shell运行python脚本。
hello.py包含:#!/usr/bin/python3
print('Hello world!')
ls -la hello.py显示:-rwxrwxr-x 1 Lexible Lexible 44 May 25 08:35 hello.py% file -k hello.py
hello.py: Python script text executable12- a /usr/bin/python3 script, UTF-8 Unicode (with BOM) text executable
% cat -e hello.py
M-oM-;M-?#!/usr/bin/python3$
print('Hello world!')$
ls -la /usr/bin/python3*显示:lrwxrwxrwx 1 root root 9 Mar 13 05:20 /usr/bin/python3 -> python3.8
-rwxr-xr-x 1 root root 5457536 Apr 27 08:53 /usr/bin/python3.8
lrwxrwxrwx 1 root root 33 Apr 27 08:53 /usr/bin/python3.8-config -> x86_64-linux-gnu-python3.8-config
lrwxrwxrwx 1 root root 16 Mar 13 05:20 /usr/bin/python3-config -> python3.8-config
-rwxr-xr-x 1 root root 384 Mar 27 19:39 /usr/bin/python3-futurize
-rwxr-xr-x 1 root root 388 Mar 27 19:39 /usr/bin/python3-pasteurize% which python
/usr/bin/python
% which python3
/usr/bin/python3
甚至。% python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type"help","copyright","credits" or"license" for more information.
>>> print('Hello world!')
Hello world!
>>>
和% python3 hello.py
Hello world!
% python hello.py
Hello world!
但是突然(多年来,我一直在运行这个python脚本)无法运行:% ./hello.py
./hello.py: 1: #!/usr/bin/python3: not found
./hello.py: 2: Syntax error: word unexpected (expecting")")
sudo apt install -f --reinstall python3 python3.8 python3-minimal python3.8-minimal libpython3.8-minimal 全部不可用。
答案1:
你可以使用。dos2unix hello.py # Install using `sudo apt install dos2unix` (if not already installed)
dos2unix将处理所有Windows/DOS类型的字符/行尾并将其转换为类Unix。
或者,也可以使用删除BOM表。sed -i '1s/^xEFxBBxBF//' hello.py
答案2:
具体地说,EF BB BF在字节23 21之前,应该是脚本的开头。./hello.py
Hello world!
相关文章