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?

3 Answers 3

17

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

0
11

+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

2
  • 2
    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
    Commented Apr 9, 2010 at 4:35
  • 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
    Commented Apr 9, 2010 at 23:44
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.