Andriken sonwane
Andriken sonwane

Reputation: 443

Permission Error Permission Denied In simple basic Flask Application

I'm running this script on vagrant based Linux system, It's a simple basic code for running flask app, I'm getting this error:

raise child_exception_type(errno_num, err_msg)
PermissionError: [Errno 13] Permission denied

Here is the script:

#!/usr/bin python3

from flask import Flask
app = Flask(__name__)

@app.route('/')
@app.route('/hello')
def helloworld():
    return 'Hello and Welcome to flask'

if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0', port=5000)

And the error I got:

vagrant@vagrant:/vagrant$ python3 project.py
* Serving Flask app "project" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "project.py", line 14, in <module>
app.run(host='0.0.0.0', port=5000)
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 943,           in run run_simple(host, port, self, **options)
File "/usr/local/lib/python3.5/dist-packages/werkzeug/serving.py", line  988, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/usr/local/lib/python3.5/dist-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "/usr/local/lib/python3.5/dist-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
File "/usr/lib/python3.5/subprocess.py", line 557, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
PermissionError: [Errno 13] Permission denied

Upvotes: 2

Views: 6824

Answers (3)

vmontazeri
vmontazeri

Reputation: 423

I had the same problem, it turns out that the port needs to be greater than 5000:

from flask import Flask, request, abort, jsonify

app = Flask(__name__)


@app.route('/getSquare', methods=['POST'])
def get_square():
    pass

    return jsonify({'answer': num ** 2})


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5001, debug=True) # make sure port is > 5000

Upvotes: 3

Dave W. Smith
Dave W. Smith

Reputation: 24966

Since you're playing in a VM that you built using Vagrant, you must have done something like

vagrant@vagrant:/vagrant$ sudo pip3 install flask

first. Possibly you installed some other packages. Depending on what base OS you're using, using pip (or pip3) to install at the system level (even in a VM) can cause problems.

After wrestling with my base install getting mangled a few time, I now always use virtualenv inside of VMs so that all of the package installs are local.

I'll bet that

vagrant@vagrant:/vagrant$ sudo apt-get install -y python-virtualenv
vagrant@vagrant:/vagrant$ virtualenv --python=python3 venv
vagrant@vagrant:/vagrant$ venv/bin/pip install flask
vagrant@vagrant:/vagrant$ ven/bin/python project.py

will give you a better result.

Upvotes: 0

Nathan
Nathan

Reputation: 8149

Maybe it's a permission error since Vagrant is trying to run your flask application and doesn't have the necessary executable permission?

I say this because of this part of your stack trace:

PermissionError: [Errno 13] Permission denied

and appears to be failing on this line of code: app.run(host='0.0.0.0', port=5000)

Perhaps you could give executable permissions on your directory containing your flask project and see if that helps resolve your issue:

chmod -R 755 /path/to/directory

Hopefully that helps!

Upvotes: 1

Related Questions