Permission Error Permission Denied In simple basic Flask Application - linux

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

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

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!

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.

Related

Permission denied while downloading dataset from Kaggle API

I am using Google cloud services and I want to download kaggle dataset. I have followed the standard ways for authentication but still, I am getting "Permission denied" error
My approach:
I have tried the following method:
(1) Installing kaggle using pip
(2) placing the kaggle.json file in /.kaggle directory
(3) ''' chmod 600 /home/rushirajparmar23000/.kaggle/kaggle.json '''
(4) ''' echo '{"username":"USERNAME","key":"KEY"}' >/root/.kaggle/kaggle.json '''
error : -bash: /root/.kaggle/kaggle.json: Permission denied
(5) kaggle competitions download -c facebook-recruiting-iii-keyword-extraction
error: raise ValueError('Error: Missing %s in configuration.' % item)
ValueError: Error: Missing username in configuration.
Maybe this discussion in the kaggle forum might answer your question:
https://www.kaggle.com/general/51898
It has a script you can run to do what you want.
When I wanted to download a dataset, I got this error. (I think it's related to your question)
Traceback (most recent call last):
File "/home/matin/.local/bin/kaggle", line 5, in <module>
from kaggle.cli import main
File "/home/matin/.local/lib/python3.9/site-packages/kaggle/__init__.py", line 19, in <module>
from kaggle.api.kaggle_api_extended import KaggleApi
File "/home/matin/.local/lib/python3.9/site-packages/kaggle/api/__init__.py", line 22, in <module>
from kaggle.api.kaggle_api_extended import KaggleApi
File "/home/matin/.local/lib/python3.9/site-packages/kaggle/api/kaggle_api_extended.py", line 84, in <module>
class KaggleApi(KaggleApi):
File "/home/matin/.local/lib/python3.9/site-packages/kaggle/api/kaggle_api_extended.py", line 102, in KaggleApi
os.makedirs(config_dir)
File "/home/matin/miniconda3/lib/python3.9/os.py", line 225, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/kaggle.json'
Solution
sudo mkdir /.kaggle
sudo cp kaggle.json /.kaggle
sudo chmod 600 /.kaggle/kaggle.json
sudo chown `whoami`: /.kaggle/kaggle.json
export KAGGLE_CONFIG_DIR='/.kaggle/'
Explanation
In KaggleApi source code, it's trying to find a directory from the environment.
config_dir = os.environ.get('KAGGLE_CONFIG_DIR') or os.path.join(
expanduser('~'), '.kaggle')
if not os.path.exists(config_dir):
os.makedirs(config_dir)
So if you add KAGGLE_CONFIG_DIR to your environment, it doesn't try to make a directory!
If you got ValueError: Error: Missing username in configuration., you should just change the owner by using chown command.
This worked for me
removed the .kaggle directory that was created when installing the kaggle API:
rm -r .kaggle/
created a new .kaggle directory in the home directory:
mkdir .kaggle
downloaded the kaggle.json file from my kaggle account:
copied the kaggle.json to .kaggle/:
cp ~/Downloads/kaggle.json .kaggle/
chmod 600 .kaggle/kaggle.json

Flask debug mode gives an "OSError: [Errno 8] Exec format error" when running using python

So, here's a file I made (flaskblog.py):
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "<h1>Home Page</h1>"
Here's how I first ran it:
$ export FLASK_APP=flaskblog.py
$ flask run
Here's how I ran it in debug mode:
$ export FLASK_APP=flaskblog.py
$ export FLASK_DEBUG=1
$ flask run
Now I want to run the application directly using python. I first updated the .py file:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "<h1>Home Page</h1>"
if __name__ == "__main__":
app.run()
This is the command I used to run the python file:
$ python3 flaskblog.py
It worked fine. Now I want to run the application in debug mode. So, I updated the file:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "<h1>Home Page</h1>"
if __name__ == "__main__":
app.run(debug=True) #Added ("debug=True") here
Command used to run the file:
$ python3 flaskblog.py
Here's the error:
* Serving Flask app "flaskblog" (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://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "flaskblog.py", line 9, in <module>
app.run(debug=True)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 943, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python3.6/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.6/dist-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "/usr/local/lib/python3.6/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.6/subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/XXX/XXX/XXX/XXX/XXX/XXX/XXX/XXX/Flask_Blog/flaskblog.py'
I just used "XXX" instead of the actual directories. Any help will be appreciated!
PS: All the code is from this video: https://www.youtube.com/watch?v=MwZwr5Tvyxo&list=PL-osiE80TeTs4UjLw5MM6OjgkjFeUxCYH
It looks like Flask is trying to run ./flaskblog.py directly for some reason, rather than with the python binary (python3 flaskblog.py), which is not working since flaskblog.py isn't executable.
So just add the following line (shebang) at the top of flaskblog.py
#!/usr/bin/env python3
...and make the file executable:
chmod +x flaskblog.py
Then try again, either with python3 flaskblog.py or directly as ./flaskblog.py.
I simply changed the permissions on the .py file from 775 to 664, essentially removing the 'x' from the permissions.
Changed it from:
-rwxrwxr-x 1 ubuntu ubuntu
to:
-rw-rw-r-- 1 ubuntu ubuntu

PermissionError: [Errno 13] Permission denied while running a server-side CGI scripts coded in Python

I am trying to run a server-side cgi script coded in python but I am getting the following error while running it.
Traceback (most recent call last):
File "webserver.py", line 16, in <module>
srvrobj = HTTPServer(srvraddr,CGIHTTPRequestHandler)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 449, in __init__
self.server_bind()
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/server.py", line 137, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 463, in server_bind
self.socket.bind(self.server_address)
PermissionError: [Errno 13] Permission denied
Here is the script that I am trying to run.
"""
Implement an HTTP web server in Python that knows how to run server-side
CGI scripts coded in Python; serves files and scripts from current working
dir; Python scripts must be stored in webdir\cgi-bin or webdir\htbin;
"""
import os,sys
from http.server import HTTPServer, CGIHTTPRequestHandler
webdir = '.'
port = 80 #default http://localhost/, else use http://localhost:xxxx/
os.chdir(webdir)
srvraddr = ("",port)
srvrobj = HTTPServer(srvraddr,CGIHTTPRequestHandler)
srvrobj.serve_forever()
Any help will be appreciated. Thanks in advance.
Use this
chmod 777 script_name,
then execute the script if it fails again then use
sudo python script_name
or check how to run script using administrator privileges on your respective operating system.

Can't create s3 resource/client in boto3

EDIT: I believe this traceback stems from some sort of issue with the dependencies. Using pip to upgrade the packages didn't work but I created a new folder and installed from scratch and that worked out
I'm a Python novice so I'm struggling to debug an AWS Lambda I'm writing.
I've narrowed it down to this line of code s3_client = botoSession.resource('s3'), which is giving a long traceback with Syntax Error: invalid syntax. The botoSession variable is just for the credentials - botoSession = boto3.session.Session(aws_access_token, aws_secret_access_token).
I've also tried s3_client = boto3.client('s3'), s3_client = boto3.resource('s3'), s3_client = botoSession.resource('s3').
When I used botoSession.client('ses', region) I had no issues sending emails.
I found Error: client = boto3.client('s3') | AWS Elastic Beanstalk Worker Environment which appeared to be a similar issue, but it appeared to be fairly old and I wasn't able to figure out what the solution was. I tried adding
import sys
sys.path = [p for p in sys.path if not p.endswith('futures-3.0.3-py3.4.egg')]
to the top of my file which didn't seem to work.
The entire traceback is as follows:
Traceback (most recent call last):
File "smartsheetExporter.py", line 45, in <module>
s3_client = botoSession.resource('s3')
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/session.py", line 389, in resource
aws_session_token=aws_session_token, config=config)
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/session.py", line 263, in client
aws_session_token=aws_session_token, config=config)
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/session.py", line 836, in create_client
client_config=config, api_version=api_version)
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/client.py", line 65, in create_client
cls = self._create_client_class(service_name, service_model)
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/client.py", line 90, in _create_client_class
base_classes=bases)
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/hooks.py", line 227, in emit
return self._emit(event_name, kwargs)
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/hooks.py", line 210, in _emit
response = handler(**kwargs)
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/utils.py", line 61, in _handler
module = import_module(module)
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/utils.py", line 52, in import_module
__import__(name)
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/s3/inject.py", line 15, in <module>
from boto3.s3.transfer import create_transfer_manager
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/s3/transfer.py", line 127, in <module>
from s3transfer.exceptions import RetriesExceededError as \
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/s3transfer/__init__.py", line 134, in <module>
import concurrent.futures
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/concurrent/futures/__init__.py", line 8, in <module>
from concurrent.futures._base import (FIRST_COMPLETED,
File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/concurrent/futures/_base.py", line 381
raise exception_type, self._exception, self._traceback
^
SyntaxError: invalid syntax
Whenever strange things are happening, it's always a good idea to update things:
sudo pip install pip --upgrade
sudo pip install boto --upgrade
sudo pip install boto3 --upgrade
sudo pip install awscli --upgrade
If you're using Python 3, try pip3 instead of pip.
I just had this same issue with boto3 and ended up having to downgrade the Python version that my lambda was running from Python 3.6 to Python 2.7. If you're using Serverless Framework for this, your serverless.yml file looks like this.
provider:
name: aws
runtime: python3.6
memorySize: 3008
cool_function:
name: cool-function
description: This lambda goes and performs magic.
handler: cool_function.lambda_handler
runtime: python2.7
- schedule:
rate: rate(4 hours)
timeout: 180
I had the same problem with python 3.6 and AWS Lambda.
I found another answer that helped me here.
You should use futures==2.2.0
If you are running your code on an Amazon EC2 instance with a Role assigned to the instance, then you only need this:
import boto3
s3_client = boto3.client('s3')
s3_resource = boto3.resource('s3') # Pick whichever is wish to use
If you are not on an Amazon EC2 instance, this works:
import boto3
session = boto3.Session(aws_access_key_id='AKIAxxx',aws_secret_access_key='yyy')
s3_client = session.client('s3')
s3_resource = session.resource('s3')
Of course, you should never put your credentials in the code file. Instead, put them in a credentials file (easiest is via aws configure) or in Environment Variables. That way, they won't be copied into any code repository.
See: Boto3 credentials

CNTK Cant load fastRCNN in wsgi with apache on windows

I've been trying to deploy a cntk model on windows with wsgi and apache.
I've got the flask server running locally and able to get results but when I try to deploy the same with apache and WSGI, it just doesn't load up.
I've tried loading an empty app in WSGI and Apache and it worked so I'm sure there's no issue with the setup, however, when I use apache, the app doesn't load up at all.
From what I could gather, wsgi is trying to load up all the libraries, one of which is fastRCNN which is locally compiled and not available in python libs.
When I comment out the import line which is importing fastRCNN, the app loads with errors such as CNTK not found.
Question:
Why is fastRCNN not importing ?
Here's what I have in my wsgi file
import sys
import os
import subprocess
from logging import Formatter, FileHandler
# Get app root, directory of .wsgi file
app_root = os.path.dirname(__file__)
command = ['activate', 'C:\local\Anaconda3-4.1.1-Windows- x86_64\envs\cntk-py35']
# Add app root to path for imports
sys.path.insert(0, app_root)
subprocess.call(command, shell=True)
os.environ['PATH'] += r'"C:\cntk\cntk;"'
python_version = sys.version_info
# Import the app
from appName import app as application
Apache conf:
LoadFile "c:/local/anaconda3-4.1.1-windows-x86_64/envs/cntk-py35/python35.dll"
LoadModule wsgi_module "c:/local/anaconda3-4.1.1-windows-x86_64/envs/cntk-py35/lib/site-packages/mod_wsgi/server/mod_wsgi.cp35-win_amd64.pyd"
WSGIPythonHome "c:/local/anaconda3-4.1.1-windows-x86_64/envs/cntk-py35"
<VirtualHost *:80 >
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias /app "C:/PATH/TO/app.wsgi"
ServerName example.com
<Directory "C:/PATH/TO/App/">
Require all granted
</Directory>
</VirtualHost>
This is the link to the code - https://github.com/Azure/ObjectDetectionUsingCntk
If I comment line no 11 in this file https://github.com/Azure/ObjectDetectionUsingCntk/blob/master/helpers.py, the app loads but with errors.
as per the suggestion, I updated apache config and now I get these errors:
mod_wsgi (pid=4180): Target WSGI script 'C:/PATH/TO/App.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=4180): Exception occurred processing WSGI script 'C:/PATH/TO/App.wsgi'.
Traceback (most recent call last):\r
File "c:\\local\\anaconda3-4.1.1-windows-x86_64\\envs\\cntk-py35\\lib\\site-packages\\cntk\\cntk_py.py", line 18, in swig_i
return importlib.import_module(mname)\r
File "c:\\local\\anaconda3-4.1.1-windows-x86_64\\envs\\cntk-py35\\lib\\importlib\\__init__.py", line 126, in import_module\
return _bootstrap._gcd_import(name[level:], package, level)\r
File "<frozen importlib._bootstrap>", line 986, in _gcd_import\r
File "<frozen importlib._bootstrap>", line 969, in _find_and_load\r
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked\r
ImportError: No module named 'cntk._cntk_py'\r
During handling of the above exception, another exception occurred:\r
Traceback (most recent call last):\r
File "C:/PATH/TO/App.wsgi", line 20, in <module>\r
from App import app as application\r
File "C:/PATH/App\\App.py", line 3, in <module>\r
from helpers_cntk import *\r
File "C:/PATH/App\\helpers_cntk.py", line 9, in <module>\r
from cntk import *\r
File "c:\\local\\anaconda3-4.1.1-windows-x86_64\\envs\\cntk-py35\\lib\\site-packages\\cntk\\__init__.py", line 10, in <modu
from . import cntk_py\r
File "c:\\local\\anaconda3-4.1.1-windows-x86_64\\envs\\cntk-py35\\lib\\site-packages\\cntk\\cntk_py.py", line 21, in <modul_cntk_py = swig_import_helper()\r
File "c:\\local\\anaconda3-4.1.1-windows-x86_64\\envs\\cntk-py35\\lib\\site-packages\\cntk\\cntk_py.py", line 20, in swig_i
return importlib.import_module('_cntk_py')\r
File "c:\\local\\anaconda3-4.1.1-windows-x86_64\\envs\\cntk-py35\\lib\\importlib\\__init__.py", line 126, in import_module\
return _bootstrap._gcd_import(name[level:], package, level)\r
ImportError: DLL load failed: The specified module could not be found.\r
These errors could perhaps be because of conda env not being setup and I'm not sure if I have the conda env setup right in wsgi script.
Thanks in advance.

Resources