Flask - WSGI - No module named 'flask'

I wasted about 25 minutes finding a solution to this and after following all the other resources I could find, I also did the following:

sudo apt-get install libapache2-mod-wsgi-py3

Make sure to have '-py3' at the end of libapache2-mod-wsgi-py3 or the runtime will default to Python2.7

Source: https://vishnut.me/blog/ec2-flask-apache-setup.html


As is polite behaviour when finding the solution, I googled around a bit more, and somehow managed to find a solution from a YouTube commment by Nathan Nichols here:

https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps

  1. Edit /etc/apache2/sites-available/FlaskApp.conf
  2. Add the following two lines before the "WSGIScriptAlias" line:

    WSGIDaemonProcess FlaskApp python-path=/var/www/FlaskApp:/var/www/FlaskApp/FlaskApp/venv/lib/python2.7/site-packages
    WSGIProcessGroup FlaskApp
    
  3. Restart Apache with "service apache2 restart"

I of course replaced the Python version with python3.5, which is what I'm running.


Nothing worked for me, but I just add below line in starting of my .py and .wsgi file voila it worked:

import sys
sys.path.append('/home/ubuntu/pythonVirtual/lib/python3.6/site-packages)

here pythonVirtual is my virtual directory for Python


First install python-virtualenv

Then from the Flask app directory where you put the wigs python files run

virtualenv env

This will create env directory inside the Flask directory and contains the local python installation.

Next activate the virtual environment by this command source env/bin/activate

After activating the virtual environment run pip install flask

Run pip --version

You will see something like this

pip 19.0.3 from /var/www/html/flaskapp/env/local/lib/python2.7/site-packages/pip (python 2.7)

Copy this python path

 WSGIDaemonProcess FlaskApp python-path=/var/www/html/flaskapp:/var/www/html/flaskapp/env/local/lib/python2.7/site-packages
WSGIProcessGroup FlaskApp

Add above lines in FlaskApp.conf in sites-available directory of apache

Note put python-path what you get after running pip --version