#problem with hosting flask appilcation

152 messages · Page 1 of 1 (latest)

karmic dock
#

Hi! A little dramatic title. Anyways, I'm trying to host my app in Render and gunicorn doesn't work 😠 .
I have specified the start commands like so:

calm epoch
#

Why are you doing __init__ as the module???

karmic dock
#
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

karmic dock
calm epoch
#

Oh... That's different than I expected

#

What's in your requirements.txt

karmic dock
#

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

calm epoch
#

I don't see gunicorn listed...

karmic dock
#

weird

#

great! imma add it, brb

#

forgot to do pip freeze > requirements.txt

#

but I had it installed

#

on my pc

calm epoch
#

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.

karmic dock
#
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'
calm epoch
#

Ok that's what I was saying before

karmic dock
#

Such an error :/

calm epoch
#

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

karmic dock
#

uhm, i don't know what do you mean by that

#

this is my init file

calm epoch
#

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.

karmic dock
#

😦

#

why?

calm epoch
#

You need to have to run it from one level above the backend folder

#

You then run it gunicorn backend:app

karmic dock
#

okay

calm epoch
#

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

karmic dock
#

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 😅

karmic dock
calm epoch
#

What's in your __init__.py?

karmic dock
calm epoch
#

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

karmic dock
#

oh

#

okay

#

time to test

#

sad that the service takes so long to build

calm epoch
#

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.

karmic dock
#

are unit tests the same as pytest?

calm epoch
#

Pytest is a unit test framework

karmic dock
#

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?

calm epoch
#

You just need to load a single page and Svelt handles all your routing for you?

#

Dunno how your frontend works

karmic dock
#

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

calm epoch
#

What error does that give you?

karmic dock
#

you mean in the browser?

calm epoch
#

Is Python giving an error?

karmic dock
#

this message should go to the console right?

calm epoch
#

If it can't access the folder I can't imagine anything useful is showing up on the front end???

karmic dock
#

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

calm epoch
#

Where is /build/ coming from?

karmic dock
calm epoch
#

Your route is /path

#

So you should be accessing everything like /thing

karmic dock
#

okay

#

so, what does that mean?

#

problem with hosting flask appilcation

calm epoch
#

Where is /build/ being inserted into your URL's???

karmic dock
#

i don't know

calm epoch
#

So you didn't put it in your code?

karmic dock
#

I haven't

calm epoch
#

What URL do you go to when you want to load your site?

karmic dock
#

into any, when I'm on localhost

calm epoch
#

On Render I mean

karmic dock
#

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

calm epoch
#

You've uploaded frontend to the server, correct?

#

Is it in the same folder as backend?

karmic dock
#

no

#

it's not in the same folder

calm epoch
#

Where is it?

karmic dock
#

in the same level where is backend

calm epoch
#

Do you start the site from within the backend folder or the folder above that?

karmic dock
#

the root directory you mean?

#

that's above

calm epoch
#

So you run it from the root folder of the repo

karmic dock
#

yes

calm epoch
#

Does it load on localhost this way?

karmic dock
#

I haven't tested, there I run it in backend/ folder

#

do you think that this could be causing it?

calm epoch
#

You should get it working correctly locally the way it needs to be for Render

karmic dock
#

damn, can't get it working locally either

calm epoch
#

I think something is weird with your home route

#

Like maybe it's not finding the folder correctly

karmic dock
#

would screensharing be helpful for solving this weird thing? I don't have a microphone, but I do have my headphones.

calm epoch
#

I don't think sooo...

So you have the folder that your project is in with the frontend and backend folders?

karmic dock
#

yes

calm epoch
#

Like this

Project Root
- frontend
- backend

And you run the gunicorn backend:app from inside of the Project Root folder?

karmic dock
#

yes

karmic dock
calm epoch
#

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

karmic dock
#

I already did

calm epoch
#

Ok, so if you go to localhost:5001/some-file-in-frontend-folder what happens?

karmic dock
calm epoch
#

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

karmic dock
#

props not

#

😅

calm epoch
#

Oh is the site not even starting

#

Your database connection is broken

karmic dock
#

WHAT

calm epoch
#

Yeah the host is set to None so psycopg2 is blowing up

karmic dock
#

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

calm epoch
#

Doesn't matter if psycopg2 is being given a host address of None

karmic dock
#

hosting is hard

#

and I'm literally out of ideas now

calm epoch
#

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

karmic dock
#

Okay, I will try to work on it.

#

that's it, imma head to sleep now, maybe I will figure it out tomorrow

karmic dock
#

I'm not getting my project working with gunicorn

#

Changed back to my local DB, then it started nagging about my flask session, okay! I added flask-session and initalized it like so, after that my OAuth broke.

karmic dock
#

Would it be possible to somehow connect frontend as static service with my backend which is an another service?

#

You are probably working right now.