Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Shebang line on Windows?

Reply
Thread Tools

Shebang line on Windows?

 
 
Walter Hurry
Guest
Posts: n/a
 
      02-22-2013
I use FreeBSD or Linux, but my son is learning Python and is using
Windows.

My question is this: Would it be good practice for him to put #!/usr/bin/
env python at the top of his scripts, so that if made executable on *nix
they will be OK? As I understand it this will have no effect on Windows
itself.

 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      02-22-2013
On 02/22/2013 01:16 PM, Walter Hurry wrote:
> I use FreeBSD or Linux, but my son is learning Python and is using
> Windows.
>
> My question is this: Would it be good practice for him to put #!/usr/bin/
> env python at the top of his scripts, so that if made executable on *nix
> they will be OK? As I understand it this will have no effect on Windows
> itself.
>


In Python 3.3 under Windows, the shebang line is useful. You run a
program called py, which examines the shebang, then loads the
appropriate version. I don't know much more, as I don't run Windows.

--
DaveA
 
Reply With Quote
 
 
 
 
Zachary Ware
Guest
Posts: n/a
 
      02-22-2013
On Fri, Feb 22, 2013 at 12:16 PM, Walter Hurry <(E-Mail Removed)> wrote:
> I use FreeBSD or Linux, but my son is learning Python and is using
> Windows.
>
> My question is this: Would it be good practice for him to put #!/usr/bin/
> env python at the top of his scripts, so that if made executable on *nix
> they will be OK? As I understand it this will have no effect on Windows
> itself.


Adding the shebang line on Windows would be excellent practice. In
fact, Python 3.3 and later ships with the Python Launcher for Windows
[1] which is very effective at reading the shebang line and executing
the script with the proper Python installation. It makes using Python
2.x and 3.x on the same Windows machine much less painful, as well as
not having to add anything to the PATH and being able to launch
whichever interpreter you want with a single command.

[1] http://docs.python.org/3/using/windo...er-for-windows
 
Reply With Quote
 
James Harris
Guest
Posts: n/a
 
      02-22-2013
On Feb 22, 6:40*pm, Zachary Ware <(E-Mail Removed)>
wrote:

> On Fri, Feb 22, 2013 at 12:16 PM, Walter Hurry <(E-Mail Removed)> wrote:


> > I use FreeBSD or Linux, but my son is learning Python and is using
> > Windows.

>
> > My question is this: Would it be good practice for him to put #!/usr/bin/
> > env python at the top of his scripts, so that if made executable on *nix
> > they will be OK? As I understand it this will have no effect on Windows
> > itself.

>
> Adding the shebang line on Windows would be excellent practice.


A word of warning unless this has since been resolved: Whenever I have
tried adding the shebang line on Windows and running it on Unix the
latter has complained about the carriage return at the end of the
line. This means that Unix does not work when invoked as follows.
(And, yes, the file has had chmod +x applied.)

./program.py

It is, of course, OK when run as

python program.py

but that removes some of the benefit of the shebang line.

James
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      02-22-2013
On 2013-02-22 22:53, James Harris wrote:
> On Feb 22, 6:40 pm, Zachary Ware <(E-Mail Removed)>
> wrote:
>
>> On Fri, Feb 22, 2013 at 12:16 PM, Walter Hurry <(E-Mail Removed)> wrote:

>
>> > I use FreeBSD or Linux, but my son is learning Python and is using
>> > Windows.

>>
>> > My question is this: Would it be good practice for him to put #!/usr/bin/
>> > env python at the top of his scripts, so that if made executable on *nix
>> > they will be OK? As I understand it this will have no effect on Windows
>> > itself.

>>
>> Adding the shebang line on Windows would be excellent practice.

>
> A word of warning unless this has since been resolved: Whenever I have
> tried adding the shebang line on Windows and running it on Unix the
> latter has complained about the carriage return at the end of the
> line. This means that Unix does not work when invoked as follows.
> (And, yes, the file has had chmod +x applied.)
>
> ./program.py
>
> It is, of course, OK when run as
>
> python program.py
>
> but that removes some of the benefit of the shebang line.
>

Just use Unix line endings. Python will accept them, and any decent editor
on Windows will accept them. (Notepad doesn't.)

 
Reply With Quote
 
Sells, Fred
Guest
Posts: n/a
 
      02-25-2013
When moving from windows to unix you need to run "dos2unix" on any programs that use shebang (at least with python 2.6) that is installed on some platforms but must be installed on others like CentOs but it is in their repository.

-----Original Message-----
From: Python-list [mailtoython-list-bounces+frsells=(E-Mail Removed)] On Behalf Of James Harris
Sent: Friday, February 22, 2013 5:53 PM
To: http://www.velocityreviews.com/forums/(E-Mail Removed)
Subject: Re: Shebang line on Windows?

On Feb 22, 6:40*pm, Zachary Ware <(E-Mail Removed)>
wrote:

> On Fri, Feb 22, 2013 at 12:16 PM, Walter Hurry <(E-Mail Removed)> wrote:


> > I use FreeBSD or Linux, but my son is learning Python and is using
> > Windows.

>
> > My question is this: Would it be good practice for him to put
> > #!/usr/bin/ env python at the top of his scripts, so that if made
> > executable on *nix they will be OK? As I understand it this will
> > have no effect on Windows itself.

>
> Adding the shebang line on Windows would be excellent practice.


A word of warning unless this has since been resolved: Whenever I have tried adding the shebang line on Windows and running it on Unix the latter has complained about the carriage return at the end of the line. This means that Unix does not work when invoked as follows.
(And, yes, the file has had chmod +x applied.)

./program.py

It is, of course, OK when run as

python program.py

but that removes some of the benefit of the shebang line.

James
--
http://mail.python.org/mailman/listinfo/python-list

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      02-25-2013


> -----Original Message-----
> From: Python-list [mailtoython-list-bounces+frsells=(E-Mail Removed)] On Behalf Of James Harris
> Sent: Friday, February 22, 2013 5:53 PM
> To: (E-Mail Removed)
> Subject: Re: Shebang line on Windows?
>
> On Feb 22, 6:40 pm, Zachary Ware <(E-Mail Removed)>
>
> A word of warning unless this has since been resolved: Whenever I have tried adding the shebang line on Windows and running it on Unix the latter has complained about the carriage return at the end of the line. This means that Unix does not work when invoked as follows.
> (And, yes, the file has had chmod +x applied.)
>
> ./program.py
>
> It is, of course, OK when run as
>
> python program.py
>
> but that removes some of the benefit of the shebang line.
>
> James
> --
> http://mail.python.org/mailman/listinfo/python-list
>


(Fixing top-posted response, so it comes after the part it is quoting)
On 02/25/2013 07:35 AM, Sells, Fred wrote:
> When moving from windows to unix you need to run "dos2unix" on any
> programs that use shebang (at least with python 2.6) that is
> installed on some platforms but must be installed on others like
> CentOs but it is in their repository.
>


It's not Python that needs dos2unix, it's bash or equivalent. For some
reason, bash shebang processing still isn't tolerant of a trailing cr on
the line. Python doesn't care.

If someone is maintaining sources that need to run on both, it's easier
to maintain them using Unix-style newlines. All it usually requires is
a decent Windows text editor that honors the existing newline
convention. Or better that can be configured to always use simple
newlines at end of each line.


--
DaveA
 
Reply With Quote
 
Chris Gonnerman
Guest
Posts: n/a
 
      02-25-2013
On 02/25/2013 06:35 AM, Sells, Fred wrote:
> When moving from windows to unix you need to run "dos2unix" on any programs that use shebang (at least with python 2.6) that is installed on some platforms but must be installed on others like CentOs but it is in their repository.

Or edit it in Vim and do

:se ff=unix

and then save it.

dos2unix is handy if you don't plan to edit the file for any other
reason. I'm assuming other editors provide similar features, but I've
been a vi/vim user FOREVER.

Or, borrowed from a Stack Overflow thread here:
http://stackoverflow.com/questions/8...return-in-unix

|sed 's/\r\n$/\n/' mymodule.py > mymodule-unix.py|


>
> -----Original Message-----
> From: Python-list [mailtoython-list-bounces+frsells=(E-Mail Removed)] On Behalf Of James Harris
> Sent: Friday, February 22, 2013 5:53 PM
> To: (E-Mail Removed)
> Subject: [Python] Re: Shebang line on Windows?
>
> On Feb 22, 6:40 pm, Zachary Ware <(E-Mail Removed)>
> wrote:
>
>> On Fri, Feb 22, 2013 at 12:16 PM, Walter Hurry <(E-Mail Removed)> wrote:
>>> I use FreeBSD or Linux, but my son is learning Python and is using
>>> Windows.
>>> My question is this: Would it be good practice for him to put
>>> #!/usr/bin/ env python at the top of his scripts, so that if made
>>> executable on *nix they will be OK? As I understand it this will
>>> have no effect on Windows itself.

>> Adding the shebang line on Windows would be excellent practice.

> A word of warning unless this has since been resolved: Whenever I have tried adding the shebang line on Windows and running it on Unix the latter has complained about the carriage return at the end of the line. This means that Unix does not work when invoked as follows.
> (And, yes, the file has had chmod +x applied.)
>
> ./program.py
>
> It is, of course, OK when run as
>
> python program.py
>
> but that removes some of the benefit of the shebang line.
>
> James
> --
> http://mail.python.org/mailman/listinfo/python-list
>



 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      02-25-2013
On Tue, Feb 26, 2013 at 12:28 AM, Chris Gonnerman <(E-Mail Removed)> wrote:
> On 02/25/2013 06:35 AM, Sells, Fred wrote:
>
>> When moving from windows to unix you need to run "dos2unix" on any
>> programs that use shebang (at least with python 2.6) that is installed on
>> some platforms but must be installed on others like CentOs but it is in
>> their repository.

>
> Or edit it in Vim and do
>
> :se ff=unix
>
> and then save it.


Or manage your files in git and set the core.autocrlf option to always
commit with Unix newlines (and you can optionally check files out with
DOS newlines, if you wish). Strongly recommended for cross-platform
work, as it "just happens" - no need to explicitly convert the file.

ChrisA
 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      02-25-2013
On 02/25/2013 06:14 AM, Dave Angel wrote:
> It's not Python that needs dos2unix, it's bash or equivalent. For some
> reason, bash shebang processing still isn't tolerant of a trailing cr on
> the line. Python doesn't care.


Actually, the shell isn't involved in parsing the shebang line at all.
That's actually done in the kernel by the program loader. So it's the
kernel that has a problem with it; wonder if Linus would accept a patch
to ignore the tailing CR?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where can I find the 'shebang' line? Ast Jay Ruby 5 07-27-2010 09:20 PM
Shebang line problems and python Blaine Python 11 09-17-2009 09:24 PM
Is "#!/usr/bin/env python" the better shebang line ? Timothy Madden Python 4 09-07-2009 08:05 AM
Shebang! line not recognized in OS X Leopard?? Johnnie Lieske Ruby 6 11-28-2008 02:25 PM
Optimize flag on shebang line Andres Corrada-Emmanuel Python 0 12-09-2003 07:02 PM



Advertisments