Python脚本:shebang行(unix)问题 [英] Python script: problems with shebang line (unix)
问题描述
我正在尝试通过向本地服务器启动测试应用程序来体验Flask微框架.尝试运行我的代码 app.py
时,我不断收到错误消息:
I am trying to get a feel for the Flask microframework by launching a test application to local server. When trying to run my code, app.py
, I keep getting the error message:
-bash: ./app.py: /flask/bin/python: bad interpreter: No such file or directory
这是基本代码(摘自此处)表示 app.py
,该代码位于我的todo-api目录中:
Here is the basic code (taken from here) for app.py
, which lives in my todo-api directory:
#!/flask/bin/python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(debug=True)
我已经检查了python解释器的文件路径,它应该存在:
I've checked the file path to the python interpreter, and it should exist:
:bin $ pwd python
Users/me/Documents/Python/todo-api/flask/bin
我已经按照教程学习了T;我尝试将shebang行更改为:
I have followed the tutorial to the T; I've tried changing the shebang line to:
#!/flask/bin/python2.x
#!flask/bin/python
#!/flask/bin/env python
但无济于事.我对bash并不了解,并且尝试查找正在发生的事情,但是对有类似问题的人的解决方案对我来说并不奏效;幕后有什么我不了解的事情吗?
But to no avail. I am not that knowledgeable about bash, and have tried looking up what is going on, but the solutions to folks with similar problems have not worked for me; is there something going on behind the scenes that I am not understanding?
推荐答案
Bash shebang期望到达解释器的绝对路径.因此,在您的情况下,您需要指定Python解释器的完整路径,即:
Bash shebangs expect an absolute path to the interpreter. So in your case you need to specify the full path to your Python interpreter i.e.:
#!/Users/me/Documents/Python/todo-api/flask/bin
您可能想研究/usr/bin/env python
的使用,以便能够使用用户的 $ PATH
环境变量中可用的解释器.参见 https://unix.stackexchange.com/questions/12736/how-does-usr-bin-env-know-which-program-to-use/12751#12751
You might want to investigate the use of /usr/bin/env python
to be able to use the interpreter that is available in your user's $PATH
environment variable. See https://unix.stackexchange.com/questions/12736/how-does-usr-bin-env-know-which-program-to-use/12751#12751
这篇关于Python脚本:shebang行(unix)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!