473,854 Members | 1,743 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 4513
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
9211
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
1349
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
1970
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
2619
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 sure if that's an indication that people don't see the restriction to top-level modules as a problem...
5
2380
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
2325
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
4318
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 of code, for example, a seperate module to handle clipboard actions, window sizing actions, etc.
5
1313
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
2941
by: prakashspatil | last post by:
what is shebang line in perl how it executes even as it starts with comment (#).
0
9901
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9750
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11024
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9510
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7909
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5738
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4550
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
2
4150
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.