473,569 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shebang or Hashbang for modules or not?

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).

Apr 11 '07 #1
13 4468
On Apr 11, 5:29 pm, "Chris Lasher" <chris.las...@g mail.comwrote:
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.

Apr 11 '07 #2
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 !-)
Apr 11 '07 #3
"Chris Lasher" <ch**********@g mail.comwrites:
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
Apr 11 '07 #4
On Thu, 12 Apr 2007 00:24:12 +0200, Bruno Desthuilliers <bd************ *****@free.quel quepart.frwrote :
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.dyn dns.org R'lyeh wgah'nagl fhtagn!
Apr 13 '07 #5
On 13 Apr 2007 10:54:18 GMT, Jorgen Grahn <gr********@sni pabacken.dyndns .orgwrote:
On Thu, 12 Apr 2007 00:24:12 +0200, Bruno Desthuilliers <bd************ *****@free.quel quepart.frwrote :
>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.dyn dns.org R'lyeh wgah'nagl fhtagn!
Apr 13 '07 #6
Jorgen Grahn a écrit :
On Thu, 12 Apr 2007 00:24:12 +0200, Bruno Desthuilliers <bd************ *****@free.quel quepart.frwrote :
>>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 ?????
Apr 13 '07 #7
On Fri, 13 Apr 2007 22:46:03 +0200, Bruno Desthuilliers <bd************ *****@free.quel quepart.frwrote :
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.dyn dns.org R'lyeh wgah'nagl fhtagn!
Apr 19 '07 #8
Jorgen Grahn a écrit :
On Fri, 13 Apr 2007 22:46:03 +0200, Bruno Desthuilliers <bd************ *****@free.quel quepart.frwrote :
>>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...
Apr 21 '07 #9
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
Apr 21 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
9192
by: deko | last post by:
In regard to running php scripts with cron - Here is a sample script: <?php //debug.php echo "<br> This is a test"; ?> I can call debug.php from a web page on my site like this:
0
1338
by: Andres Corrada-Emmanuel | last post by:
Hello, Does the optimize flag work on the shebang line of UNIX systems? I've tried to execute a script on a Solaris system with a shebang line of the form: #! /usr/bin/env python -O and it does not recognize the flag but
0
1941
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is inside a package, the invocation will fail with a "Module not found" error. This PEP is aimed at fixing that :) Cheers, Nick.
15
2570
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that for Python 2.5. Previously, posting of a draft version of the PEP to python-dev and python-list didn't actually generate any responses. I'm not...
5
2369
by: rbt | last post by:
Haven't tested this on Windows yet... thought I'd ask here: Does the line below have any negative impact on Windows machines? I develop and test mostly on Unix, but my scripts are often used on Win systems too. #!/usr/bin/env python Many thanks,
11
2303
by: Joerg Schuster | last post by:
Hello, is there a way to compile a python file foo.py to foo.pyc (or foo.pyo) such that foo.pyc can be run with 'foo.pyc' (as opposed to 'python foo.pyc') on the command line? Jörg Schuster
7
4293
by: Lauren Quantrell | last post by:
At running the risk of asking how big is too big... Is there a rule of thumb or a best practice that says I may have too many modules? I currently have a Access2K app with about 30 code modules, plus of course the modules associated with the forms themselves. My practice has been to create a seperate module to handle specific groupings...
5
1303
by: veracon | last post by:
Long story short, in order to use Python 2.5, I've compiled it in my own account on my hosting. It works fantastic as /home/my_username/python2.5, but the shebang is a bit long. Is there a way to shorten it (environment variables?) or, even better, make /usr/bin/env python point to it? Thanks in advance!
2
2932
by: prakashspatil | last post by:
what is shebang line in perl how it executes even as it starts with comment (#).
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.