问题描述
我在一个mac,我写了相当多的python脚本。
每次我需要运行它们时,我必须输入' python script_name.py
'。是否有我的方式使它,所以我只需要键入像' p script_name.py
'?这将节省一些时间:D
我假设你从命令行运行你的脚本?如果是,请在脚本中添加以下行作为第一行:
#!/ usr / bin / python
或
#!/ usr / bin / env python
code> / usr / bin ,然后在Unix /终端提示符处输入以下命令(它使你的脚本可执行):
chmod + x script_name.py
那么在您只需要在命令提示符处键入脚本的名称即可运行它。不需要 python
部分命令。也就是
./ script_name.py
您也可以使用别名$ c
> alias p =python
c>〜/ .bashrc 文件
I'm on a mac, and I write quite a bit of python scripts.
Every time I need to run them, I have to type 'python script_name.py
'. Is there I way to make it so I only have to type like 'p script_name.py
'? It would save some time :D
I am assuming you are running your script from the command line right? If so, add the following line as the first line in your script:
#!/usr/bin/python
or alternatively
#!/usr/bin/env python
in case the python command is not located in /usr/bin
, and then issue the following command once at the Unix/terminal prompt (it makes your script "executable"):
chmod +x script_name.py
from then on you only need to type the name of the script at the command prompt to run it. No python
part of the command needed. I.e., simply
./script_name.py
will run the script.
You can also of course go with the alias
, but the above is a cleaner solution in my opinion.
For the alias
alias p="python"
should go into your ~/.bashrc
file
这篇关于如何别名命令行命令? (苹果电脑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!