/usr/bin 中的 Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1168042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:38:15  来源:igfitidea点击:

Python scripts in /usr/bin

pythonunixscripting

提问by Steve Gattuso

I'm writing a pretty basic application in python (it's only one file at the moment). My question is how do I get it so the python script is able to be run in /usr/bin without the .py extension?

我正在用 python 编写一个非常基本的应用程序(目前只有一个文件)。我的问题是如何获取它以便 python 脚本能够在没有 .py 扩展名的情况下在 /usr/bin 中运行?

For example, instead of running

例如,而不是运行

python htswap.py args

from the directory where it currently is, I want to be able to cd to any directory and do

从它当前所在的目录,我希望能够 cd 到任何目录并执行

htswap args

Thanks in advance!

提前致谢!

回答by Thomas

Simply strip off the .pyextension by renaming the file. Then, you have to put the following line at the top of your file:

只需.py通过重命名文件去除扩展名。然后,您必须将以下行放在文件的顶部:

#!/usr/bin/env python

envis a little program that sets up the environment so that the right pythoninterpreter is executed.

env是一个设置环境以便python执行正确解释器的小程序。

You also have to make your file executable, with the command

您还必须使用命令使您的文件可执行

chmod a+x htswap

And dump it into /usr/local/bin. This is cleaner than /usr/bin, because the contents of that directory are usually managed by the operating system.

并将其转储到/usr/local/bin. 这比 干净/usr/bin,因为该目录的内容通常由操作系统管理。

回答by Michiel Buddingh

The first line of the file should be

文件的第一行应该是

#!/usr/bin/env python

You should remove the .pyextension, and make the file executable, using

您应该删除.py扩展名,并使文件可执行,使用

chmod ugo+x htswap

EDIT:Thomaspoints out correctly that such scripts should be placed in /usr/local/binrather than in /usr/bin. Please upvote his answer (at the expense of mine, perhaps. Seven upvotes (as we speak) for this kind of stuff is ridiculous)

编辑:托马斯指出正确的,这样的脚本应该放在/usr/local/bin而不是/usr/bin。请赞成他的回答(可能是以我的为代价。对这种东西的七次赞成(正如我们所说的那样)是荒谬的)

回答by rlazo

Shebang?

舍邦?

#!/usr/bin/env python

Put that at the beginning of your file and you're set

把它放在文件的开头,你就设置好了

回答by b3rx

add #!/usr/bin/env pythonto the very top of htswap.pyand rename htswap.pyto htswapthen do a command: chmod +x htswapto make htswap executable.

添加#!/usr/bin/env python到最顶端的htswap.py,并重新命名htswap.py,以htswap然后执行命令:chmod +x htswap使htswap可执行文件。

回答by rand_acs

I see in the official Python tutorials, http://docs.python.org/tutorial/index.html, that

我在官方 Python 教程http://docs.python.org/tutorial/index.html中看到,

#! /usr/bin/env python

is used just as the answers above suggest. Note that you can also use the following

就像上面的答案所建议的那样使用。请注意,您还可以使用以下

#!/usr/bin/python

This is the style you'll see for in shell scripts, like bash scripts. For example

这是您将在 shell 脚本(如 bash 脚本)中看到的样式。例如

#!/bin/bash

Seeing that the official tuts go with the first option that is probably your best bet. Consistency in code is something to strive for!

看到官方 tuts 选择第一个选项,这可能是您最好的选择。代码的一致性是需要努力的!