问题
I've been following Sentdex' Flask tutorial. He's using a Venv to set up his Flask, but didn't set his Python up to work with a Venv. I've tried installing Flask globally - yet it still doesn't work. Trying to browse to the server returns a 500 Internal Server Error
I'm getting the usual no module named flask
error.
errorFGL.log
[Sun Feb 05 11:22:32.043925 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] mod_wsgi (pid=26340): Target WSGI script '/var/www-fgl/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Sun Feb 05 11:22:32.044105 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] mod_wsgi (pid=26340): Exception occurred processing WSGI script '/var/www-fgl/FlaskApp/flaskapp.wsgi'.
[Sun Feb 05 11:22:32.044243 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] Traceback (most recent call last):
[Sun Feb 05 11:22:32.045011 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] File "/var/www-fgl/FlaskApp/flaskapp.wsgi", line 8, in <module>
[Sun Feb 05 11:22:32.045070 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] from FlaskApp import app as application
[Sun Feb 05 11:22:32.045549 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] File "/var/www-fgl/FlaskApp/FlaskApp/__init__.py", line 1, in <module>
[Sun Feb 05 11:22:32.045594 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] from flask import Flask
[Sun Feb 05 11:22:32.045689 2017] [wsgi:error] [pid 26340:tid 118578538694400] [client 86.52.205.25:49814] ImportError: No module named 'flask'
__init__.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def homepage():
return "Success"
if __name__ == "__main__":
app.run()
flaskapp.wsgi
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www-fgl/FlaskApp/")
from FlaskApp import app as application
application.secret_key = '[REDACTED]'
fgl-database.conf
<VirtualHost *:80>
ServerName [REDACTED]
WSGIScriptAlias / /var/www-fgl/FlaskApp/flaskapp.wsgi
<Directory /var/www-fgl>
Require all granted
</Directory>
Alias /static /var/www-fgl/FlaskApp/FlaskApp/static
<Directory /var/www-fgl/FlaskApp/FlaskApp/static/>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/errorFGL.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/accessFGL.log combined
</VirtualHost>
回答1:
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
- Edit
/etc/apache2/sites-available/FlaskApp.conf
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
- Restart Apache with "service apache2 restart"
I of course replaced the Python version with python3.5, which is what I'm running.
回答2:
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
回答3:
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
回答4:
According to http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html.
You should edit
/etc/apache2/sites-available/FlaskApp.conf
Add these above the
WSGIScriptAlias
WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp
WSGIProcessGroup FlaskApp
I met the same problem and fix it by the way.
来源:https://stackoverflow.com/questions/42050982/flask-wsgi-no-module-named-flask