Command-Line

執行python腳本而不聲明它解釋器

  • May 10, 2018

我有這樣一個程序可以從命令行檢查數據的方法:

me at me in ~/Desktop/Coding/codes
$ cat check_methods.py
#! /usr/bin/env python
from sys import argv
methods = dir(eval(argv[1]))
methods = [i for i in methods if not i.startswith('_')]
print(methods)

me at me in ~/Desktop/Coding/codes
$ python check_methods.py list
['append', 'clear', 'copy', 'count', 'extend', 'index',
   'insert', 'pop', 'remove', 'reverse', 'sort']

me at me in ~/Desktop/Coding/codes
$ python check_methods.py dict
['clear', 'copy', 'fromkeys', 'get', 'items', 'keys',
   'pop', 'popitem', 'setdefault', 'update', 'values']

我想直接從 bash 執行程序,例如:

$ check_methods.py list
-bash: check_methods.py: command not found

如何實現呢?

指定腳本的路徑,因為它不在$PATH.

./check_methods.py list

並且永遠不要添加.$PATH.

引用自:https://unix.stackexchange.com/questions/442929