Shebang或Hashbang的模块与否?

编程入门 行业动态 更新时间:2024-10-28 20:31:02
本文介绍了Shebang或Hashbang的模块与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

不打算执行的Python模块是否应该使用shebang / hashbang(例如,#!/ usr / bin / env python)?我习惯在每个.py文件中都有一个 shebang,但是我最近听到有人认为 shebangs只适用于那些意图是 可执行文件(即从命令行运行)。

解决方案

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

如果没有打算执行的Python模块有shebang / hashbang (例如,#!/ usr / bin / env python)或不?我习惯在每个.py文件中都有一个 shebang,但是我最近听到有人认为 shebangs只适用于那些意图是 可执行文件(即从命令行运行)。

如果你不打算让模块可执行,那么添加一个shebang 行和/或设置文件执行位是两者都违背 预定用途。因此,您应该省略shebang /不设置 执行位以强调您的预期用途。 在开发过程中,预期用途可能与使用时不同 已部署。 - Paddy。

Chris Lasheraécrit: 如果不打算执行的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...@gmailwrote:

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的模块与否?

本文发布于:2023-10-20 21:18:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1512154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   Shebang   Hashbang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!