0

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

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)

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

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

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?

5
  • From the bash prompt, run type python and report the results. Commented Jul 20, 2016 at 22:04
  • @John1024 the result of type python yields the result: python is hashed (/usr/bin/python) Commented Jul 20, 2016 at 22:06
  • Then try #!/usr/bin/python. Commented Jul 20, 2016 at 22:06
  • @John1024 I've done this, and it also doesn't work; I think this is referring to the place where my system-wide python interpreter is installed. Following the tutorial, I used virtualenv Commented Jul 20, 2016 at 22:07
  • can you try sed -n l app.py ? It is for displaying non-visible characters Commented Jul 20, 2016 at 22:07

3 Answers 3

3

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

If you don't know the actual path of the interpreter you are using (in the case that your assumption is incorrect) try inspecting sys.executable in any code that you run.
Thanks, this has resolved the issue. I didn't realize that I needed the full path to call the interpreter I had set up in the virtual environment. I am now dealing with a separate issue with Flask, but I'll take some time to look into that. Thanks again
1

pwd tells you the current directory. 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. Put that path into your shebang line. Note that this will give you the Python interpreter from your $PATH, which may or may not be the right one.

The standard shebang line for Python scripts is

#!/usr/bin/env python

or

#!/usr/bin/python

1 Comment

Thanks for the info on pwd. Unfortunately, when I try which python it shows me where my system-wide python interpreter is installed. For the flask tutorial, I've used virtualenv
1

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. 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.

Here's what I did (only takes a couple of minutes and no config): First; pyinstaller needs: build-essential & python-dev

apt-get install build-essential python-dev
(or yum, etc... depending on your OS)

Then use the built in python package manager to setup pyinstaller: pip install pyinstaller

That's it. Run: pyinstaller --onefile myapp.py (or pyinstaller.exe if your OS needs 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.