将应用程序或脚本转换为Shell命令 [英] Turn an application or script into a shell command
问题描述
当我想从命令行运行python应用程序时(在ubuntu下),我必须位于源代码app.py所在的目录中,并使用命令
When I want to run my python applications from commandline (under ubuntu) I have to be in the directory where is the source code app.py and run the application with command
python app.py
我该怎么做(通常是怎么做的)使用以下命令从任意目录运行应用程序: app
?类似地,当您键入 ls
, mkdir
和其他命令时?
How can I make it (how is it conventionally done) to run the application from arbitrary directory with the command: app
? Similarly as you type ls
, mkdir
and other commands?
谢谢
推荐答案
将脚本所在的目录添加到您的路径中,使其可执行并添加适当的shebang
Add the directory that the script is in to your path, make it executable, and add a proper shebang line.
在您的 .bashrc
中:
PATH=$PATH:/dir/to/the/script
可执行文件:
chmod +x myscript.py
在脚本顶部,添加shebang行:
At the top of the script, add the shebang line:
#!/usr/bin/env python
然后,您可以在任何地方执行以下操作:
Then, from anywhere, you can just do:
myscript.py
(请注意,您不需要.py后缀,可以将其称为任何东西,例如 app
,如果您有适当的shebang行)。
(Note that you don't need a .py suffix, it could be called anything, e.g. app
if you have a proper shebang line).
这篇关于将应用程序或脚本转换为Shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!