Home > front end >  How set path of folder and files in Gunicorn. ( wsgi.py )
How set path of folder and files in Gunicorn. ( wsgi.py )

Time:07-13

this my flask module main.py

import os 
import Flask
import pandas as pd

app = Flask(__name__)

@app.route("/") # this route is working 
def index():
    return "this working"

@app.route("/data", methods=["POST"])
def get_data():
    json = request.json
    df = pd.DataFrame(json)
    #here some other code that work on the data that are geting from folderspath as we have define below 
    
if __name__=="__main__":
    path=os.path.join(os.path.abspath(os.path.join('data')))
    folder=os.path.join(path,'test')
    app.run(debug=Flase,host="0.0.0.0")

if we just run the flask server it work and execute path statment but if we set for deployment purpose and using gunicorn the first route work. but when we send request to second route it give error of missing folder that are mention in paths. the below module (wsgi.py) is not getting those path how to set these path, that work in wsgi.py

my Gunicorn file wsgi.py

from main import app
if __name__=="__main__":
    app.run()  
    

i want wsgi.py to execute those path before app.run() i tried to put in those statement in wsgi before app.run() and imported dependences but still not working. any help would be appreciated. thanks

CodePudding user response:

You could try gunicorn's --pythonpath argument:

--pythonpath STRING   A comma-separated list of directories to add to the Python path.

CodePudding user response:

Hi @Mark Bailey i did not get ( gunicorn --pythonpath ), could you please elaborate this with example.
i did this way and it work for me
main.py

# in **main.py** i jsut commented the paths and copied it and paste in **wsgi.py**

#then i import path  variables from **wsgi.py** in **main.py** like
from wsgi.py import path , folder

wsgi.py

# in wsgi.py i imported these paths before if statement

import os # added
from main import app
path=os.path.join(os.path.abspath(os.path.join('data'))) # added
folder=os.path.join(path,'test')# added

if __name__=="__main__":
    app.run()

this worked for me . is this good approach?

  • Related