[FIXED] 'No module named flask', when I run through a URL

Issue

My files are placed as below in the apache shared hosting server –

/public_html/cgi-bin
 --- myenv (directory - virtual env)
 --- hello.cgi
 --- myapp.py

the .htaccess is in the root directory.

Now when I activate the virtual environment and run the myapp.py file –

python myapp.py

this works fine. I get this –

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

But now when I try to launch my page in the web browser, I get the below Import error in the logs (on the web page its 500 – Internal Server Error) –

 'No module named flask'

Please suggest if I need to give the path of the flask package anywhere in the .cgi or the .py file.

flask is installed in the myenv : myenv/lib/python2.6/site-packages

myapp.py file:

#!/usr/bin/env python
import sys

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello Ejaz"

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

hello.cgi file:

#!/usr/bin/env python

import os
from wsgiref.handlers import CGIHandler
from myapp import app

os.environ['SERVER_NAME'] = "www.mypage.com"
os.environ['SERVER_PORT'] = ""
os.environ['REQUEST_METHOD'] = 'GET'
os.environ['PATH_INFO'] = ""

CGIHandler().run(app)

Thank you.

Solution

Refer here – http://www.comfycoder.com/home/how_to_deploy_a_flask_app_in_apache_shared_hosting

updated my hello.cgi to this-

#!/usr/bin/env python

import os
from wsgiref.handlers import CGIHandler
from myapp import app

CGIHandler().run(app)

and added these lines in myapp.py

import os
import sys
sys.path.insert(0, '/home/username/public_html/cgi-bin/myenv/lib/python2.6/site-packages') 

It works fine.

Answered By – Ejaz

Answer Checked By – Katrina (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published