Problem executing python executable

I am trying to create a program to run it through the command line in the following format:

./myProgram 

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

 env: python\r: No such file or directory 

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

+4
source share
3 answers

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

+17
source

+1 as suggested by ignacio.

however, to answer the first part of your question more directly, each OS / system uses a different line ending 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 the old Mac OS 8 or 9 uses only CR or \r .

to solve this problem, you can run the utility, as ignacio suggested, or you should be able to do this from your text editor (perhaps this is not very obvious).

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

when you tell the shell of your OS to execute it, you need to analyze this line and execute any interpreter you requested, but if this is not possible, it will intimidate you, as it were.

hope this helps!

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

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

here is a brief description of other related attributes that I plagiarized from the hardcore Python intro-course notes : alt text

+11
source

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

+3
source

Source: https://habr.com/ru/post/1306437/


All Articles