如何使用自定义命令从命令行执行 python 脚本? [英] How do I execute a python script from the command line using custom commands?

查看:51
本文介绍了如何使用自定义命令从命令行执行 python 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的术语被关闭,我提前道歉,但我想做的是使用自定义命令执行 python 脚本.例如,我想编写一个接受一些 shell 参数并执行一些任意 shell 命令的 python 脚本.我们将此脚本称为 random.py.而不是键入:

I apologize in advance if my terminology is off, but what I want to do is execute a python script using a custom command. So for example, I want to write a python script that accepts some shell arguments and performs some arbitrary shell commands. We'll call this script random.py. Instead of typing:

> python random.py arguments go here

我希望能够输入以下内容:

I would like to be able to type something like:

> random arguments go here

所以换句话说,我想去掉 python 命令并省略 .py.我意识到我可以使用别名,但我也想让其他任何想要使用它的人都可以使用这个脚本,我不想让他们创建别名.

So in other words, I want to get rid of the python command and omit the .py. I realize that I could use an alias but I also want to make this script available for anyone else that wants to use it, and I don't want to ask them to make an alias.

基本上我所追求的是诸如meteor js、zurb 基础或grunt 之类的东西.安装meteor后,我可以通过进入shell并输入以下内容来创建一个新应用程序:

Basically what I'm after is something like meteor js, zurb foundation, or grunt. After I installed meteor I was able to create a new application by going to the shell and typing:

> meteor create --newapplicationname 

与基础和咕噜声相同的概念.这就是我所追求的功能类型.任何关于如何做这种事情的资源将不胜感激.

Same concept with foundation and grunt. That's the type of functionality I'm after. Any resources on how to do that sort of thing would be greatly appreciated.

推荐答案

对我有用的 (Ubuntu) 是像这样在/usr/local/bin 中放置一个符号链接,

What worked for me (Ubuntu) is putting a symbolic link in /usr/local/bin like this,

$> sudo ln -s /path/to/your/python/script/random.py /usr/local/bin/random
$> sudo chmod 755 /usr/local/bin/random

(如果您想自动执行此步骤,只需将这两个命令放在 bash 脚本中).通常终端现在应该找到脚本并执行它,如果你只输入

(If you want to automate this step just put these two commands in a bash script). Normally the terminal should now find the script and execute it if you just type

$> random arg1 arg2

只要确保你的python脚本以

Just make sure your python script starts with

#!/usr/bin/python

这篇关于如何使用自定义命令从命令行执行 python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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