Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Shebang or Hashbang for modules or not?

Reply
Thread Tools

Shebang or Hashbang for modules or not?

 
 
Chris Lasher
Guest
Posts: n/a
 
      04-11-2007
Should a Python module not intended to be executed have shebang/
hashbang (e.g., "#!/usr/bin/env python") or not? I'm used to having a
shebang in every .py file but I recently heard someone argue that
shebangs were only appropriate for Python code intended to be
executable (i.e., run from the command line).

 
Reply With Quote
 
 
 
 
Paddy
Guest
Posts: n/a
 
      04-11-2007
On Apr 11, 5:29 pm, "Chris Lasher" <(E-Mail Removed)> wrote:
> Should a Python module not intended to be executed have shebang/
> hashbang (e.g., "#!/usr/bin/env python") or not? I'm used to having a
> shebang in every .py file but I recently heard someone argue that
> shebangs were only appropriate for Python code intended to be
> executable (i.e., run from the command line).


If you don't intend the module to be executable then adding a shebang
line and/or setting the files execute bit are both contrary to
intended use. You should therefore leave out the shebang/not set the
execute bit to emphasise your intended use.
During development however, intended use may differ from use when
deployed.

- Paddy.

 
Reply With Quote
 
 
 
 
Ben Finney
Guest
Posts: n/a
 
      04-11-2007
"Chris Lasher" <(E-Mail Removed)> writes:

> I recently heard someone argue that shebangs were only appropriate
> for Python code intended to be executable (i.e., run from the
> command line).


Since that's the purpose of putting in a shebang line, that's what I'd
argue also; specifically:

- Modules intended primarily for import should be named 'foo.py' and
have no shebang line.

This doesn't preclude having an 'if __name__ == "__main__":'
block, and running the module as 'python ./foo.py' for whatever
reason.

- Modules intended primarily for running as a command should have a
shebang line and an 'if __name__ == "__main__":' block, and be
named according to the conventions of the OS for naming command
programs; on *nix, this means naming the file 'foo'.

This doesn't preclude importing the module (e.g. for unit
testing), though it is a little more convoluted than a simple
'import' statement.

--
\ "Dad always thought laughter was the best medicine, which I |
`\ guess is why several of us died of tuberculosis." -- Jack |
_o__) Handey |
Ben Finney
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      04-11-2007
Chris Lasher a écrit :
> Should a Python module not intended to be executed have shebang/
> hashbang (e.g., "#!/usr/bin/env python") or not?


The shebang is only useful for files that you want to make directly
executable on a *n*x system. They are useless on Windows, and not
technically required to use the file as a main program -ie: you can
always run it like this:
$ /path/to/python filename.py

> I'm used to having a
> shebang in every .py file


An encoding declaration might be more useful IMHO !-)
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      04-13-2007
On Thu, 12 Apr 2007 00:24:12 +0200, Bruno Desthuilliers <(E-Mail Removed)> wrote:
> Chris Lasher a écrit :
>> Should a Python module not intended to be executed have shebang/
>> hashbang (e.g., "#!/usr/bin/env python") or not?

>
> The shebang is only useful for files that you want to make directly
> executable on a *n*x system. They are useless on Windows,


Probably (unless setup.py uses them for something meaningful there,
too). But of course often you don't know that the file will always be
used only on Windows, or that the Windows user won't prefer Cygwin.

> and not
> technically required to use the file as a main program -ie: you can
> always run it like this:
> $ /path/to/python filename.py


You can, but sometimes it's not appropriate. If you distribute a
Python program to Unix users in that form, they may not want to know
or care which language it's written in. Especially if you decide, a
few releases later, that you want to switch to Perl or something.

I realise that you took a more narrow view than I do above, so
please see this as additional notes rather than critisism. It's just
that I am struggling with people at work who feel program names
should encode whatever language they happen to be written in, and so
I am a bit oversensitive ...

>> I'm used to having a
>> shebang in every .py file

>
> An encoding declaration might be more useful IMHO !-)


They are not mutually exclusive, if that is what you mean.
I always use both.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      04-13-2007
On 13 Apr 2007 10:54:18 GMT, Jorgen Grahn <(E-Mail Removed)> wrote:
> On Thu, 12 Apr 2007 00:24:12 +0200, Bruno Desthuilliers <(E-Mail Removed)> wrote:
>> Chris Lasher a écrit :
>>> Should a Python module not intended to be executed have shebang/
>>> hashbang (e.g., "#!/usr/bin/env python") or not?

>>
>> The shebang is only useful for files that you want to make directly
>> executable on a *n*x system. They are useless on Windows,

>
> Probably (unless setup.py uses them for something meaningful there,
> too).


There's another, secondary, reason to use a shebang on Python source
which isn't executable: the Unix file(1) command and friends can see
that it's Python code.

(Of course, such files will almost always be named foo.py.)

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      04-13-2007
Jorgen Grahn a écrit :
> On Thu, 12 Apr 2007 00:24:12 +0200, Bruno Desthuilliers <(E-Mail Removed)> wrote:
>
>>Chris Lasher a écrit :
>>
>>>Should a Python module not intended to be executed have shebang/
>>>hashbang (e.g., "#!/usr/bin/env python") or not?

>>
>>The shebang is only useful for files that you want to make directly
>>executable on a *n*x system. They are useless on Windows,

>
>
> Probably (unless setup.py uses them for something meaningful there,
> too). But of course often you don't know that the file will always be
> used only on Windows, or that the Windows user won't prefer Cygwin.


From a practical POV, I consider cygwin as a *n*x system. And FWIW, I
mentionned this platform specificness because it might not be clear to
anyone reading the OP.

>
>>and not
>>technically required to use the file as a main program -ie: you can
>>always run it like this:
>>$ /path/to/python filename.py

>
>
> You can, but sometimes it's not appropriate.


That's another question. What I meant here is that you don't technically
need the shebang and x bit to allow execution of a Python file. IOW, the
shebang is only meaningfull for python files intented to be effectivly
used as a program.

> If you distribute a
> Python program to Unix users in that form, they may not want to know
> or care which language it's written in. Especially if you decide, a
> few releases later, that you want to switch to Perl or something.


<troll>
No one in it's own mind would decide to switch from Python to Perl !-)
</troll>

More seriously, and as far as I'm concerned, when I want to make a
python script (by opposition to a python 'module') available as a unix
command, I either use a symlink or a shell script calling the python
script.

> I realise that you took a more narrow view than I do above,


I mostly tried to answer the OP question.

> so
> please see this as additional notes rather than critisism. It's just
> that I am struggling with people at work who feel program names
> should encode whatever language they happen to be written in,


OMG.

> and so
> I am a bit oversensitive ...


I can understand this...

>
>>>I'm used to having a
>>>shebang in every .py file

>>
>>An encoding declaration might be more useful IMHO !-)

>
>
> They are not mutually exclusive, if that is what you mean.


Of course not. But one is always usefull (and will become more or less
mandatory in a near future IIRC), while the other is either totally
useless (module files) or only partially useful (script files).

> I always use both.


Even in modules ?????
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      04-19-2007
On Fri, 13 Apr 2007 22:46:03 +0200, Bruno Desthuilliers <(E-Mail Removed)> wrote:
> Jorgen Grahn a écrit :

....
>> If you distribute a
>> Python program to Unix users in that form, they may not want to know
>> or care which language it's written in. Especially if you decide, a
>> few releases later, that you want to switch to Perl or something.

>
> <troll>
> No one in it's own mind would decide to switch from Python to Perl !-)
> </troll>


I was trolling a bit, too

Actually, it made sense in my case. It was a typical Perl task -- a
filter regex-parsing a huge (a few hundred megabytes) text file.
Rewriting it in Perl for speed was faster and more readable than
rewriting it in Python for speed.

> More seriously, and as far as I'm concerned, when I want to make a
> python script (by opposition to a python 'module') available as a unix
> command, I either use a symlink or a shell script calling the python
> script.


A symlink yes, but a shell script? Wouldn't it be easier to write a
one-liner (well, two-liner) Python script in that case?

>>>>I'm used to having a
>>>>shebang in every .py file
>>>
>>>An encoding declaration might be more useful IMHO !-)


....

>> I always use both.

>
> Even in modules ?????


Yes, for a few reasons:
- file(1) can tell it's Python source
- I tend to leave unit tests in my modules
- I just started doing that when I first tried Python;
it's part of my mental boilerplate

I don't claim they are good reasons. And since I strongly dislike
setting the execute bit on things that aren't executable, I should
probably stop using the shebang everywhere, too ...

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      04-21-2007
Chris Lasher wrote:
> Should a Python module not intended to be executed have shebang/
> hashbang (e.g., "#!/usr/bin/env python") or not? I'm used to having a
> shebang in every .py file but I recently heard someone argue that
> shebangs were only appropriate for Python code intended to be
> executable (i.e., run from the command line).


Personally I include it in all of them, as part of boilerplate in a
template.
--
Michael Hoffman
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      04-21-2007
Jorgen Grahn a écrit :
> On Fri, 13 Apr 2007 22:46:03 +0200, Bruno Desthuilliers <(E-Mail Removed)> wrote:
>
>>Jorgen Grahn a écrit :

>

(snip)
>
>>More seriously, and as far as I'm concerned, when I want to make a
>>python script (by opposition to a python 'module') available as a unix
>>command, I either use a symlink or a shell script calling the python
>>script.

>
>
> A symlink yes, but a shell script? Wouldn't it be easier to write a
> one-liner (well, two-liner) Python script in that case?


Not necessarily. Just like there are cases where it makes sens to use
Perl instead of Python, some things are really far simpler to do with
shell scripts...
 
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
Hashbang error pradeepbpin Python 3 06-01-2010 11:59 AM
shebang in cross platform scripts rbt Python 5 04-06-2005 05:33 PM
Optimize flag on shebang line Andres Corrada-Emmanuel Python 0 12-09-2003 07:02 PM
Re: shebang strange thing... Dan Bishop Python 19 06-29-2003 10:30 PM



Advertisments