1

Every time I edit a Python script with VSC it ruins the shebang. If I manually fix it via sed or even nano, it works but if I edit anything in the script with VSC(any line) and then try to execute the script, I get the bad interpreter error ./marshal.py: bad interpreter: /bin/python3^M: no such file or directory.

Anyone have any ideas ?
Edit: So I am editing this script in Windows via VSC but executing it in WSL. Fixing the shebang in WSL works but as soon as I edit it with VSC again it breaks the shebang

WaXxX333
  • 388
  • 1
  • 2
  • 11
  • 1
    That ^M makes me think you have configured the wrong end of line (windows instead of linux) – Gonzalo Odiard Dec 03 '21 at 03:57
  • What do you mean ? Right now I'm executing the script in WSL but editing it in Windows with VSC. If I fix the shebang in WSL it will execute properly but as soon as I edit any part of the script in VSC it breaks the shebang – WaXxX333 Dec 03 '21 at 04:18
  • I think @GonzaloOdiard hinted in the right direction. UNIX and Windows OS use different End-Of-Line character, '\n' for UNIX vs '\r\n' for Windows. Try addind `"files.eol": "\n",` to your VSC settings.json, to force using the UNIX one. – asiera Dec 03 '21 at 07:09

1 Answers1

1

This is a problem with different line endings. Windows uses CRLF (carriage return + line feed) for line endings, while Linux uses LF (line feed only). The additional carriage return in your Windows-saved file is unexpected when executed in Linux and produces the given error.

VS Code displays the configured line ending for the current file in the lower right corner of the status bar:

enter image description here

Clicking on it lets you change it to LF:

enter image description here

After saving the file it is executable in a Linux environment.

carlfriedrich
  • 2,919
  • 1
  • 15
  • 29
  • 1
    And as a side note: maybe you should consider using the [WSL remote development](https://code.visualstudio.com/docs/remote/wsl) infrastructure of VS code. This would have prevented the issue in the first place and opens up much more convenient possibilites. – carlfriedrich Dec 03 '21 at 12:07