Shebang或Hashbang的模块与否? [英] Shebang or Hashbang for modules or not?

查看:52
本文介绍了Shebang或Hashbang的模块与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不打算执行的Python模块是否应该使用shebang /

hashbang(例如,#!/ usr / bin / env python)?我习惯在每个.py文件中都有一个

shebang,但是我最近听到有人认为

shebangs只适用于那些意图是

可执行文件(即从命令行运行)。

解决方案

4月11日下午5:29, Chris Lasher < chris.las ... @ gmail.comwrote:


如果没有打算执行的Python模块有shebang /

hashbang (例如,#!/ usr / bin / env python)或不?我习惯在每个.py文件中都有一个

shebang,但是我最近听到有人认为

shebangs只适用于那些意图是

可执行文件(即从命令行运行)。



如果你不打算让模块可执行,那么添加一个shebang

行和/或设置文件执行位是两者都违背

预定用途。因此,您应该省略shebang /不设置

执行位以强调您的预期用途。

在开发过程中,预期用途可能与使用时不同

已部署。


- Paddy。


Chris Lasheraécrit:
< blockquote class =post_quotes>
如果不打算执行的Python模块是否有shebang /

hashbang(例如,#!/ usr / bin / env python)或不?



shebang仅对您想要直接制作的文件有用

可执行* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *它们在Windows上没用,而且在技术上不需要使用该文件作为主程序-b:你可以

总是像这样运行:


/ path / to / python filename.py


我习惯拥有
$ b $每个.py文件中的b shebang



编码声明可能更有用恕我直言! - )


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

解决方案

On Apr 11, 5:29 pm, "Chris Lasher" <chris.las...@gmail.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.


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


这篇关于Shebang或Hashbang的模块与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆