正如其他有说,你需要把"shebang"放在文件的开头,说哪个解释器用来执行文件。
如在上述链路提到的,最简单的方法是使用env
命令(而不是固定的路径至python
) - 把这个作为文件中的第一行:
#!/usr/bin/env python
壳将在$PATH
中查找您的python
,而不是查找/usr/local/bin/python
,然后失败。这意味着如果Python安装在非标准位置,它将会工作。
例如:现在
$ cat example.py
print "Test"
$ file example.py # it is treated as an ASCII file
example.py: ASCII text
$ chmod +x example.py
$ ./example.py # when executed, it defaults to being executed as a shell script
./example.py: line 1: print: command not found
,如果我添加“认领”行...
$ cat example.py
#!/usr/bin/env python
print "Test"
$ file example.py # it is recognised as a Python script
example.py: a python script text executable
$ ./example.py # and executes correctly
Test
来源
2009-09-13 19:13:12
dbr
哦,如果它是一个基于文本的程序,“xterm -e python”会引出一个终端。替换您选择的终端仿真器。 – kwatford 2009-09-14 02:37:09