#problem with hosting flask appilcation
152 messages · Page 1 of 1 (latest)
Why are you doing __init__ as the module???
Mar 12 09:18:45 PM ==> Uploading build...
Mar 12 09:19:04 PM ==> Build uploaded in 16s
Mar 12 09:19:04 PM ==> Build successful 🎉
Mar 12 09:19:04 PM ==> Deploying...
Mar 12 09:19:21 PM ==> Starting service with 'gunicorn __init__:app'
Mar 12 09:19:21 PM bash: gunicorn: command not found
command line
wdym?
attrs==22.2.0
Authlib==1.2.0
cachetools==5.2.0
certifi==2022.12.7
cffi==1.15.1
charset-normalizer==2.1.1
click==8.1.3
cryptography==39.0.0
exceptiongroup==1.1.0
Flask==2.2.2
Flask-Cors==3.0.10
flask-oidc==1.4.0
Flask-SQLAlchemy==3.0.2
google-api-core==2.11.0
google-api-python-client==2.70.0
google-auth==2.15.0
google-auth-httplib2==0.1.0
google-auth-oauthlib==0.8.0
googleapis-common-protos==1.57.0
greenlet==2.0.1
httplib2==0.21.0
idna==3.4
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
oauth2client==4.1.3
oauthlib==3.2.2
packaging==23.0
pluggy==1.0.0
protobuf==4.21.12
psycopg2-binary==2.9.5
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.21
pyparsing==3.0.9
pytest==7.2.2
python-dotenv==0.21.1
requests==2.28.1
requests-oauthlib==1.3.1
rsa==4.9
six==1.16.0
SQLAlchemy==1.4.44
tomli==2.0.1
uritemplate==4.1.1
urllib3==1.26.13
Werkzeug==2.2.2
I don't see gunicorn listed...
weird
great! imma add it, brb
forgot to do pip freeze > requirements.txt
but I had it installed
on my pc
I use poetry instead of pip because it prevents issues like that. Can be a bit harder to get working cleanly on a server though.
Mar 12 09:27:50 PM ==> Uploading build...
Mar 12 09:28:11 PM ==> Build uploaded in 17s
Mar 12 09:28:12 PM ==> Build successful 🎉
Mar 12 09:28:12 PM ==> Deploying...
Mar 12 09:28:32 PM ==> Starting service with 'gunicorn __init__:app'
Mar 12 09:28:39 PM Traceback (most recent call last):
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/util.py", line 402, in import_app
Mar 12 09:28:39 PM app = getattr(mod, name)
Mar 12 09:28:39 PM AttributeError: module '__init__' has no attribute 'app'
Mar 12 09:28:39 PM
Mar 12 09:28:39 PM During handling of the above exception, another exception occurred:
Mar 12 09:28:39 PM
Mar 12 09:28:39 PM Traceback (most recent call last):
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/bin/gunicorn", line 8, in <module>
Mar 12 09:28:39 PM sys.exit(run())
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
Mar 12 09:28:39 PM WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/base.py", line 231, in run
Mar 12 09:28:39 PM super().run()
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/base.py", line 72, in run
Mar 12 09:28:39 PM Arbiter(self).run()
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/arbiter.py", line 58, in __init__
Mar 12 09:28:39 PM self.setup(app)
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/arbiter.py", line 118, in setup
Mar 12 09:28:39 PM self.app.wsgi()
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
Mar 12 09:28:39 PM self.callable = self.load()
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
Mar 12 09:28:39 PM return self.load_wsgiapp()
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
Mar 12 09:28:39 PM return util.import_app(self.app_uri)
Mar 12 09:28:39 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/util.py", line 406, in import_app
Mar 12 09:28:39 PM raise AppImportError("Failed to find attribute %r in %r." % (name, module))
Mar 12 09:28:39 PM gunicorn.errors.AppImportError: Failed to find attribute 'app' in '__init__'.
Mar 12 09:28:50 PM ==> Starting service with 'gunicorn __init__:app'
Ok that's what I was saying before
Such an error :/
Is your app laid out like a package where imports for files are import package_name.file_name or did you do import file_name only???
You have a bunch of Python files, right? When you import them do you import them as backend.views or as just views?
Ohh
You're using relative imports
Ok
So
You cannot start your site from inside of the backend folder.
You need to have to run it from one level above the backend folder
You then run it gunicorn backend:app
okay
You never run __init__.py it's run when you import the folder it's in
You're importing like this from .views import views
That dot is a relative import
It will only work inside of a package and is the same as from backend.views import views
okay, makes sense
I'll see if the build succeeds
the other guy, who I was kinda following was making a new file, outside of that folder and there, he imported the app from that package. And in the build command he put gunicorn file:app, so I thought I'm cleaver and ran the app from inside the backend folder directly, but I guess my way of doing stuff doesn't really work out huh 😅
oh, a new error!
Mar 12 09:40:05 PM Traceback (most recent call last):
Mar 12 09:40:05 PM File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/util.py", line 402, in import_app
Mar 12 09:40:05 PM app = getattr(mod, name)
Mar 12 09:40:05 PM AttributeError: module 'backend' has no attribute 'app'
Mar 12 09:40:05 PM
btw it's my first time deploying anything 😅
I'm probably annoying as hell for you haha
What's in your __init__.py?
it should be readable enough
App needs to be a global
I'd return it from your function and just do app = create_app() at the end of the file
Personally I'd move all the stuff from __init__.py to app.py. Then you'd run it with gunicorn backend.app:app
This way you can import everything without needing to create the app.
Helpful if you ever wanna add unit tests.
are unit tests the same as pytest?
Pytest is a unit test framework
oh, okay, idk what that means
🙂
looks like my deploy was successful
but now I gotta get my frontend working as well
somehow
:/
any good idea how to get frontend working too?
You just need to load a single page and Svelt handles all your routing for you?
Dunno how your frontend works
pretty much how I would also imagine it, the problem now is that
@views.route("/<path:path>")
def home(path):
return send_from_directory('../frontend/public', path)
is responsible for my frontend, but it looks like my server can't access frontend/ directory. If my talk makes any sense
What error does that give you?
you mean in the browser?
Is Python giving an error?
this message should go to the console right?
If it can't access the folder I can't imagine anything useful is showing up on the front end???
idk, in the logs it doesn't tell like anything would be wrong
only in the browser console it says:
oivaline-rakendus.onrender.com/:14
GET https://oivaline-rakendus.onrender.com/build/bundle.js net::ERR_ABORTED 404```
okay, so I compared the two applications via sources in google dev tools
Where is /build/ coming from?
Where is /build/ being inserted into your URL's???
i don't know
So you didn't put it in your code?
I haven't
What URL do you go to when you want to load your site?
into any, when I'm on localhost
On Render I mean
is that what you mean?
I want to be helpful, but I have 0 experiance with this stuff so I don't even know how to be helpful
You've uploaded frontend to the server, correct?
Is it in the same folder as backend?
Where is it?
Do you start the site from within the backend folder or the folder above that?
So you run it from the root folder of the repo
yes
Does it load on localhost this way?
I haven't tested, there I run it in backend/ folder
do you think that this could be causing it?
You should get it working correctly locally the way it needs to be for Render
damn, can't get it working locally either
I think something is weird with your home route
Like maybe it's not finding the folder correctly
would screensharing be helpful for solving this weird thing? I don't have a microphone, but I do have my headphones.
I don't think sooo...
So you have the folder that your project is in with the frontend and backend folders?
yes
Like this
Project Root
- frontend
- backend
And you run the gunicorn backend:app from inside of the Project Root folder?
yes
it does return an error, but that's not important
Ok, so unless Flask is doing something weird, ../frontend is actually doing leaving the Project Root and trying to access frontend outside of it.
Some Parent Folder
- Project Root
- frontend
- backend
- frontend (what the `../frontend` is going to)
So get rid of the ../
I already did
Ok, so if you go to localhost:5001/some-file-in-frontend-folder what happens?
I didn't get it working with gunicorn, so I can't go to that url
Yeah, that's what I'm trying to debug
That should cause your home route to run
Even if it doesn't exist
Or can't find it
Need to figure out if your home route is running
What it's getting
https://pastebin.com/xGRDFK7z
idk if that helps
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
props not
😅
WHAT
Yeah the host is set to None so psycopg2 is blowing up
it DOES run when I start the application from backend/ folder
omg, is it the env file what's messed up now?
nope
:/
my DB is up and running
Doesn't matter if psycopg2 is being given a host address of None
Nah, the hosting is correct. The site just isn't setup correctly.
You need to get everything working locally the same way it'll be on Render.
So running it from the root folder, etc.
Once it's working locally it'll work on Render since you've got the hosting setup already
Okay, I will try to work on it.
that's it, imma head to sleep now, maybe I will figure it out tomorrow