Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

A strange error on running Python script at the shell

📅 2013-Jun-03 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ env, error, newline, python, shell ⬩ 📚 Archive


I have a Python script named Since, I would like to run it directly from the shell, I have added a shebang to run it through Python:

#!/usr/bin/env python
print "Hello world"

If I run it directly at the shell, I get a strange error:

$ ./
: No such file or directory

However, if I run it explicitly with the Python interpreter, it works fine:

$ python
Hello world


This strange error turned out to be due to the newline markers in the file. The file was created on Windows and had CR+LF as the newline marker. But, I was running the script on Linux, which only uses LF as the newline marker.

So, this must have led the env program to run the character CR as a command. And obviously, it could not find any such file or directory.

The solution is simple. Convert the newline markers in the file to that of the host system, in this case to Linux. This can be done easily in Vim by using the fileformat option.

Tried with: GNU CoreUtils 8.12.197, Python 2.7.3 and Ubuntu 12.04 LTS

© 2023 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 Mastodon📧 Email