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

Question:

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.

Asked By: Ejaz

||

Answers:

You’ve specified the system python in the first line of your cgi file. Instead, point it to the python executable of your virtualenv.

Answered By: ganduG

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

I have an additional question on this. This approach works fine if there is 1 flask website. The moment I add another website webspace with all the files in a separate folder, the other webiste on browser opening says – no module named flask.

Answered By: Satya
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.