tags:

views:

101

answers:

4

I'm trying to make a program so that I can run it through the command line with the following format:

./myProgram

I made it executable and put #!/usr/bin/env python in the header, but it's giving me the following error.

env: python\r: No such file or directory

However, when I run "python myProgram", it runs fine. Can someone tell me what I'm doing wrong?

+9  A: 

Your line endings are wrong. Use dos2unix to fix them.

Ignacio Vazquez-Abrams
A: 

At first, I thought your shebang line, #!/usr/bin/env python was missing a slash or something... but I then learned that env is a program which figures out where the program listed thereafter is to be found. So... the shebang line seems ok! Gotta be something with the unix vs. DOS line terminations as mentioned in Ignacio Vazquez-Abrams's answer.

mjv
Nope. `env` is a program that searches the entries in `$PATH` for the executable name passed to it.
Ignacio Vazquez-Abrams
@Ignacio Vazquez-Abrams: Oops, bad guess. I'll edit the answer but leave it it, for "educational purposes"
mjv
+2  A: 

dos2unix filename.py or inside vim issue the command :set fileformat=unix and save.

puyo
+6  A: 

+1 on ignacio's suggestion.

however, to answer the 1st part of your question more directly, each OS/system uses a different line termination character:

POSIX (any Unix-flavor like Linux, *BSD, Mac OS X, etc.) uses \n (NEWLINE) while DOS/Win uses the combo \r\n (CR/carriage return + NEWLINE) and old Mac OS 8 or 9 uses just CR or \r.

to solve this problem, you can run a utility like ignacio has suggested, or you should be able to do it from your text editor (may not be very apparent however).

to answer the other part of your question, the reason why $ python myProgram works is because Python treats all three different line endings the same... the shebang line at the top is ignored because you told Python to load and run that script, and "#" means the first line is a comment and thus ignored.

when you tell your OS shell to run it, it needs to parse that line and execute whatever interpreter you requested, but if it can't, it pukes on you like it did.

hope this helps!

ps. on a side note, you can find out what line-termination character is being used on your operating system, just check out the os.linesep (data) attribute. for example, on my Mac (OS X), i get this:

>>> import os
>>> os.linesep
'\n'

here's a quick summary of the other related attributes that i plagiarized from my hardcore Python intro course notes: alt text

wescpy
I like the table, and I like the fact that you're careful to point out that there's such a thing as "old" Mac and "new" Mac, and they're different.
John Y
great, glad you like it JohnY! yeah, i used to use this table in my Python courses but removed it when Universal Read was added, which i forgot to mention:if you open text files to process but don't want to use `os.linesep` to help you parse the file, just open it with `'U'` and all line endings will be treated as if they were `\n`, e.g., `f = open('data.txt', 'U')`
wescpy