user3151858
user3151858

Reputation: 820

Import Error: No module named Flask

I am getting 500 internal server error when running my server with Flask,apache2, and Python 3.4.2.

At first I installed Flask without creating virtual environment, so I thought that that could be a reason. However, I later created virtual environment for my app using:

python3.4 -m venv venv
source venv/bin/activate
(venv) pip3 install Flask

I checked by running my app using python3 __init.py and it was working on local. Then I reloaded my apache2 server, and still getting the same error. Please see below output from error log file:

mod_wsgi (pid=25667): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=25667): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
Traceback (most recent call last):
File "/var/www/FlaskApp/flaskapp.wsgi", line 11, in <module>
from XYZ import app as application
File "/var/www/FlaskApp/XYZ/__init__.py", line 1, in <module>
from flask import Flask, render_template
ImportError: No module named flask

Any suggestions how to fix it?

FlaskApp.conf file in the etc/apache2/sites-available

<VirtualHost *:80>

ServerName xyz.com

ServerAlias www.xyz.com

ServerAdmin [email protected]
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/XYZ/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/XYZ/static
<Directory /var/www/FlaskApp/XYZ/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here's line from apache2 error log file:

AH00489: Apache/2.4.10 (Ubuntu) mod_wsgi/3.5 Python/2.7.8 configured -- resuming normal operations

It looks like mod_wsgi is trying to use 2.7.8 as opposed to 3.4. How do I fix that?

Upvotes: 0

Views: 10609

Answers (3)

FrozZerrer
FrozZerrer

Reputation: 292

I changed my xx.wsgi from using execfile() to using exec(). Here is what it looks like when it finally worked.

activate_this = '/opt/flask/project_name/py3venv/bin/activate_this.py'
exec(open(activate_this).read(), dict(__file__=activate_this))

import sys
sys.path.insert(0, '/opt/flask/project_name')

from project_app_name import app as application

Upvotes: 2

user3151858
user3151858

Reputation: 820

I think I finally solved it! I added the following line of code to FlaskApp.conf file located in etc/apache2/sites-available:

WSGIPythonPath /var/www/FlaskApp/XYZ/venv/:/var/www/FlaskApp/XYZ/venv/lib/python3.4/site-packages

This line should go ahead of VirtualHost!

Then I restarted apache and got my site working, except only with www, non-www does not work.

Upvotes: 2

Hamms
Hamms

Reputation: 5107

mod_wsgi likely doesn't know about your virtualenv, and simply needs to be told to use that rather than the default system environment.

This info in the Django docs should help.

If it doesn't, please post your flaskapp.wsgi and httpd.conf files.

Upvotes: 0

Related Questions