Linux

'No such file or directory' error when executing a Python file

The following error is reported when running the Python file: Check the first line of the file: Running the command directly on the command line produces no err

The following error is reported when running the Python file:

env: python3\r: No such file or directory

Check the first line of the file:

#!/usr/bin/env python3  
# -*- coding: utf-8 -*-

Running the command directly on the command line produces no errors: /usr/bin/env python3

Python 3.10.6 (main, Aug 11 2022, 13:49:25) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

So I called upon the great god Google and found the original post:

what could cause a script to fail to find python when it has #!/usr/bin/env python in the first line?

The reason is that the file was modified on Windows, which adds '\r' at the end of each line. As is well known, Unix files end lines with '\n', while Windows uses '\n\r'. Unix/Unix-like systems naturally cannot recognize the extra '\r'.

Solution:

Delete the extra '\r'

The article mentions using the tool dos2unix and the stream-editing tool sed. Commands are as follows: dos2unix /path/to/file Or sed -i 's/\r$//' /path/to/file

To avoid being accused of mindlessly copying, here is the dos2unix installation command

For macOS: brew install dos2unix

For Gentoo: emerge -avt dos2unix

For Centos: yum install -y dos2unix[^1]

[^1] It's said that CentOS 8 switched to dnf; I'm an old-timer who has never used it—man it yourself.

For Debian/Ubuntu: apt-get update && apt-get install dos2unix

For Others: No, go ask Baidu.

N
norvyn

独立 iOS 开发者,写字的人。在一座有海的城市,慢慢地做一些小而确定的东西。An independent iOS developer and writer — slowly making small, certain things in a city by the sea.

评论Comments

加载中…Loading…

留下评论Leave a comment