The file could not be found in a pythonanywhere hosted application
Here’s an answer – File not found on pythonanywhere.com
But that didn’t work for me.
I’ve put the files in the project home directory and /project/static
directory –
My code-
from flask import Flask, render_template, request
from flask_cors import CORS, cross_origin
from diffdiag import DifferentialDiagScript2 as dd
import os
app = Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
cors = CORS(app, resources={r"/*": {"origins": "*"}})
@app.route('/')
@cross_origin()
def index():
module_dir = os.path.dirname(__file__)
file_path = os.path.join(module_dir, 'literature.csv')
items = list(line.strip() for line in open(file_path))
return render_template('index.html', table = items)
I also tried-
items = list(line.strip() for line in open(f2))
items = list(line.strip() for line in open('./static/f2'))
items = list(line.strip() for line in open('/home/daddyodevil/add/Automated_DD/f2'))
items = list(line.strip() for line in open('/home/daddyodevil/add/Automated_DD/static/f2'))
Nothing seems to work.
Thanks for any help.
Edit 1 – Add a full error
Exception on / [GET]#012Traceback (most recent call last):#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app#012 response = self.full_dispatch_request()#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request#012 rv = self.handle_ user_exception(e)#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function#012 return cors_after_request( app.make_response(f(*args, **kwargs)))#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception#012 reraise(exc_type, exc_value, tb)#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise#012 raise value#012 File "/home/daddyodevil/.local/lib/ python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request#012 rv = self.dispatch_request()#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/ flask/app.py", line 1799, in dispatch_request#012 return self.view_functions[rule.endpoint](**req.view_args)#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/ flask_cors/decorator.py", line 128, in wrapped_function#012 resp = make_response(f(*args, **kwargs))#012 File "/home/daddyodevil/add/Automated_DD/app.py", line 13, in index#012 items = list(line.strip() for line in open("./static/f2"))#012FileNotFoundError: [Errno 2] No such file or directory: './static/f2'
Edit 2 – Add the error you receive when trying to run app.py
in pythonanywhere
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "/home/daddyodevil/add/Automated_DD/app.py", line 47, in <module>
app.run()
File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/app.py", line 943, in run
run_simple(host, port, self, **options)
File "/home/daddyodevil/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 814, in run_simple
inner()
File "/home/daddyodevil/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 774, in inner
fd=fd)
File "/home/daddyodevil/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 660, in make_server
passthrough_errors, ssl_context, fd=fd)
File "/home/daddyodevil/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 577, in __init__
self.address_family), handler)
File "/usr/lib/python3.6/socketserver.py", line 453, in __init__
self.server_bind()
File "/usr/lib/python3.6/http/server.py", line 136, in server_bind
socketserver. TCPServer.server_bind(self)
File "/usr/lib/python3.6/socketserver.py", line 467, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
Solution
Running app.py in the PythonAnywhere console is unlikely to work — it will try to bind (bind) to a port and listen for connections, and it is likely that someone else is using that port.
The problem you see in the website error log is because you tried to open the file ‘./static/f2'
. The path resolves relative to the directory where the application runs, which is not necessarily the same directory that contains the module, or it may not be the home directory.
If you want to load one in the same directory as the module, and you already have code that calculates the module directory like this
module_dir = os.path.abspath(os.path.dirname(__file__))
… You can then change your code to use that module_dir
by changing the line
items = list(line.strip() for line in open("./static/f2"))
… Be this:
items = list(line.strip() for line in open(os.path.join(module_dir, "./static/f2")))