[英]Python script: problems with shebang line (unix)
I am trying to get a feel for the Flask microframework by launching a test application to local server. 我试图通过向本地服务器启动测试应用程序来感受Flask微框架。 When trying to run my code, app.py
, I keep getting the error message: 尝试运行代码app.py
,我不断收到错误消息:
-bash: ./app.py: /flask/bin/python: bad interpreter: No such file or directory
Here is the basic code (taken from here) for app.py
, which lives in my todo-api directory: 这是app.py
的基本代码(从此处获取) ,位于我的todo-api目录中:
#!/flask/bin/python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(debug=True)
I've checked the file path to the python interpreter, and it should exist: 我已经检查了python解释器的文件路径,它应该存在:
:bin $ pwd python
Users/me/Documents/Python/todo-api/flask/bin
I have followed the tutorial to the T; 我已经按照教程学习了T; I've tried changing the shebang line to: 我尝试将shebang行更改为:
#!/flask/bin/python2.x
#!flask/bin/python
#!/flask/bin/env python
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; 我对bash并不了解,并且尝试查找正在发生的事情,但是对于遇到类似问题的人来说,解决方案对我没有用; is there something going on behind the scenes that I am not understanding? 幕后是否有我不了解的事情?
Bash shebangs expect an absolute path to the interpreter. Bash shebangs期望获得通往口译员的绝对路径。 So in your case you need to specify the full path to your Python interpreter ie: 因此,在您的情况下,您需要指定Python解释器的完整路径,即:
#!/Users/me/Documents/Python/todo-api/flask/bin
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. 您可能想研究/usr/bin/env python
,以便能够使用用户的$PATH
环境变量中可用的解释器。 See https://unix.stackexchange.com/questions/12736/how-does-usr-bin-env-know-which-program-to-use/12751#12751 参见https://unix.stackexchange.com/questions/12736/how-does-usr-bin-env-know-which-program-to-use/12751#12751
pwd
tells you the current directory. pwd
告诉您当前目录。 It doesn't tell you where a command is located. 它不会告诉您命令的位置。 The output from that command is a red herring. 该命令的输出是红色鲱鱼。
You may be looking for which python
. 您可能正在寻找which python
。 Put that path into your shebang line. 将该路径放入您的shebang行中。 Note that this will give you the Python interpreter from your $PATH
, which may or may not be the right one. 请注意,这将为您提供$PATH
的Python解释器,它可能是正确的,也可能不是正确的。
The standard shebang line for Python scripts is Python脚本的标准shebang行是
#!/usr/bin/env python
or 要么
#!/usr/bin/python
I was having a similar issue with trying to setup a python script as an executable for testing some things and realized that bash was getting in the way more than it was helping. 我在尝试将python脚本设置为可执行文件以测试某些东西时遇到了类似的问题,并意识到bash的作用远不止于帮助。 I ended up setting up pyinstaller (which is incredibly easy) and then making my script a stand alone executable without bash being in the mix. 我最终设置了pyinstaller(这非常简单),然后使我的脚本成为一个独立的可执行文件,而无需担心bash的问题。
Here's what I did (only takes a couple of minutes and no config): First; 这是我所做的(仅需几分钟,无需配置): pyinstaller needs: build-essential & python-dev pyinstaller需要:build-essential和python-dev
apt-get install build-essential python-dev apt-get install build-essential python-dev
(or yum, etc... depending on your OS) (或yum等...,具体取决于您的操作系统)
Then use the built in python package manager to setup pyinstaller: pip install pyinstaller 然后使用内置的python软件包管理器来设置pyinstaller: pip install pyinstaller
That's it. 而已。 Run: pyinstaller --onefile myapp.py (or pyinstaller.exe if your OS needs exe) 运行:pyinstaller --onefile myapp.py(如果操作系统需要exe,则运行pyinstaller.exe)。
If it's successful (and it usually is), your new executable will be in a folder "Dist" in the same area you ran pysinstaller. 如果成功(通常是成功的话),您的新可执行文件将在运行pysinstaller的同一区域的“ Dist”文件夹中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.