简体   繁体   中英

Issue with making python program executable

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?

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

+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

dos2unix filename.py或在vim内部发出命令:set fileformat=unix并保存。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM