Re: [Tutor] Confusion with Python, Bash and Command Prompt
On Aug 10, 2012, at 12:20 AM, Steven D'Aprano wrote: > [byte] > That is not Python's doing. That is the shell, and so it depends > entirely on your choice of operating system and shell. It works on Unix, > Linux and probably Mac OS, but not on Windows. > Yes, it definitely does work on Mac OS-X as well. -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Confusion with Python, Bash and Command Prompt
On 10/08/12 15:35, Modulok wrote: ... My Question: Is it true that doing that is as same as doing #!/usr/bin/env python on Unix? Because I think that the matter of shebang is limited to Bash and Windows don't have a bash, it has a Command Prompt. And I don't think such thing happens in Windows. It has nothing directly do with bash. It has to do with the operating system's 'program loader'. Correction noted, thank you. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Confusion with Python, Bash and Command Prompt
On 08/10/2012 12:01 AM, Santosh Kumar wrote: > Hello There, > > We all know that line starting with "#" in Python is a comment. > > We also know that when the first line of any file (with any extension) > has "#!/usr/bin/env bash" and if you make it executable and do > ./filename.ext in the terminal then it will be considered as bash file > (in this case even if the extension is.py the terminal will take it as > bash file.) > > This same thing also happens with Python (If the first line of the > file has #!/usr/bin/env python then even if the extension is .txt, if > you make it an executable then terminal will process it with python > interpreter.) > > So far we know two things: > 1. "#" makes the line a comment in Python. > 2. If processing file with bash, no matter what the file extension is, > if the first line of that file hsa "#!/usr/bin/env python" and it is > an executable and you run it doing like ./filename.ext, the bash will > call python interpreter to process that file. > > Now let's get back. I'm reading a book "A Byte of Python" > (http://www.swaroopch.org/notes/Python). In this page: > http://www.swaroopch.org/notes/Python_en:First_Steps, under the > section Using a Source File. There is a line saying that "A word of > caution on the shebang..". On the next line the book says we can use > #!C:\Python32\python.exe to make it executable. > > My Question: > Is it true that doing that is as same as doing #!/usr/bin/env python > on Unix? Because I think that the matter of shebang is limited to Bash > and Windows don't have a bash, it has a Command Prompt. And I don't > think such thing happens in Windows. Nothing to do with Windows, or bash really. It's a question of who interprets the line you're typing, and then how that program decides which program to run. Unix-like systems have one set of conventions, but there's no problem breaking them if you don't mind upsetting users. Windows has a different convention, and there's no problem there in breaking it. The standard shell for Windows NT systems is called cmd.exe (It was command.com for MSDOS). But many other shells have been written for Windows, both free and commercial. It's likely that at least one of them processes the shebang line according to Unix conventions. Perhaps your book has available such an alternative shell. Cygwin can be installed into Windows. I would expect that Cygwin comes with its own shell. However, once you're using that one, you probably don't use filenames with colons inside them. I haven't used Windows much in the last few years, but seems to me Cygwin mapped the C: drive into some place like /mnt/c_drive If I have it even close, then the shebang line would look something like: #!/mnt/c_drive/Python32/python.exe -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Confusion with Python, Bash and Command Prompt
... > My Question: > Is it true that doing that is as same as doing #!/usr/bin/env python > on Unix? Because I think that the matter of shebang is limited to Bash > and Windows don't have a bash, it has a Command Prompt. And I don't > think such thing happens in Windows. It has nothing directly do with bash. It has to do with the operating system's 'program loader'. It's the low level code responsible for setting up an execution environment for a new process. It looks for environment variables, etc. For example on FreeBSD most shebang parsing defined in envopts.c. Basically, whenever you request a file to be executed, the program loader (not your shell) looks at the first line of the file for a shebang. If found, it executes the file the shebang line references and passes the path to the current script file as the first argument. That said, different operating systems' program loaders', regardless of the shell you're using (be it bash, tcsh, sh, etc), will process shebang lines differently. Some ignore them entirely. The syntax can even vary slightly from one OS to another (and not just the file paths). So does it apply to Windows? No. Windows has no concept of shebang lines. File types are determined exclusively their extensions, as is the executability of a file. (There's a registry looking that occurs to determine what program to pass the file to.) If you rename a python script from foo.py to foo.jpg, windows will attempt to open it in an image viewer, regardless of any shebang lines present. (Contrast this with most unix-like flavors where extensions are meaningless. Executability is determined by a file-system level permission bit and the interpreter is determined by the program loader reading shebang lines. The file would be passed to the python interpretter, assuming a correct shebang.) You are right in your assumption; It does not apply to Windows. The tutorial is incorrect. -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Confusion with Python, Bash and Command Prompt
On 10/08/12 14:01, Santosh Kumar wrote: Hello There, We all know that line starting with "#" in Python is a comment. We also know that when the first line of any file (with any extension) has "#!/usr/bin/env bash" and if you make it executable and do ./filename.ext in the terminal then it will be considered as bash file (in this case even if the extension is.py the terminal will take it as bash file.) That is not Python's doing. That is the shell, and so it depends entirely on your choice of operating system and shell. It works on Unix, Linux and probably Mac OS, but not on Windows. As far as Python is concerned, "#!/usr/bin/env python" is just a meaningless comment. Now let's get back. I'm reading a book "A Byte of Python" (http://www.swaroopch.org/notes/Python). In this page: http://www.swaroopch.org/notes/Python_en:First_Steps, under the section Using a Source File. There is a line saying that "A word of caution on the shebang..". On the next line the book says we can use #!C:\Python32\python.exe to make it executable. The book is completely wrong there. Windows does not pay any attention to shebang lines. Although, soon, Python itself will provide a launcher for Windows which understands shebang lines: http://www.python.org/dev/peps/pep-0397/ -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor