问题描述
我正在用 python 编写一个非常基本的应用程序(目前只有一个文件).我的问题是如何获取它以便 python 脚本能够在没有 .py 扩展名的情况下在/usr/bin 中运行?
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 htswap.py args
从当前所在的目录,我希望能够 cd 到任何目录并执行
from the directory where it currently is, I want to be able to cd to any directory and do
htswap args
提前致谢!
推荐答案
只需通过重命名文件去除 .py
扩展名.然后,您必须将以下行放在文件顶部:
Simply strip off the .py
extension by renaming the file. Then, you have to put the following line at the top of your file:
#!/usr/bin/env python
env
是一个设置环境的小程序,以便执行正确的 python
解释器.
env
is a little program that sets up the environment so that the right python
interpreter is executed.
您还必须使用命令使您的文件可执行
You also have to make your file executable, with the command
chmod a+x htswap
并将其转储到 /usr/local/bin
.这比 /usr/bin
更简洁,因为该目录的内容通常由操作系统管理.
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/bin 中的 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!