I have a Python file that is developed under Windows, so it naturally has CR LF line endings. I added the Python shebang on it using #!/usr/bin/env python3
to make it directly executable under Linux. The loader prints out an error message:
/usr/bin/env: ‘python3\r’: No such file or directory
I know I can simply execute the file using python3 x.py
, but for various reasons I want to have it with the #!
on the first line.
My current solution is to have the first line in a separate file that has Unix line endings, and use cat prefix.py x.py > y.py
to generate an executable with mixed line endings. But I would prefer to have the first line being handled differently by ViM. Is that possible?
If I edit y.py
I get a ^M
on each but the first line since this is a mixed line endings file, so a solution for me would be to not display the ^M
and use DOS line endings everywhere, but keep the UNIX line ending on the first line.
The easy and obvious solution is to run dos2unix on the python script and strip out the insidious \r character.
The hackish solution is to create a symlink literally named /usr/local/bin/python^M which points to /usr/local/bin/python. This will let you run all such python scripts in the future without running dos2unix on them first.
Source: https://natanyellin.com/posts/shebang-python-bad-interpreter-m/