I am trying to deploy a Flask project on Ubuntu 16 server (Apache2). In this project, I am using Python3 libraries. So I want to set Flask to use Python 3 on the server. But I am really having a terrible time. Here is what I am doing:
sudo apt-get install apache2
sudo apt-get update
sudo apt-get install libapache2-mod-wsgi-py3 # I think that is how you install wsgi for python3
sudo apt-get install python-flask
sudo apt-get upgrade
my project conf: /etc/apache2/sites-available/project.conf
<VirtualHost *:80>
ServerName 52.25.54.241 #my IP
ServerAdmin [email protected]
WSGIScriptAlias / /var/www/FlaskApps/FlaskApps.wsgi
<Directory /var/www/FlaskApps/project/>
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/FlaskApps/project/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
and finally: /var/www/FlaskApps/FlaskApps.wsgi
#! /usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApps/project/")
# home points to the home.py file
from home import app as application
application.secret_key = "somesecretsessionkey"
NOTE: When I install wsgi like this:
sudo apt-get install libapache2-mod-wsgi
it works, but it uses python2. and When I install wsgi 3. It does not work and it says no module called flask. So how can I set flask to use python3 by default?
I read this question: Getting Flask to use Python3 (Apache/mod_wsgi) but it didn't help me. It is not clear for me because they are using virtualenv.
To change to python3, you can use the following command in terminal alias python=python3 . But that only work for the current running process in terminal.
In summary, making flask use python3, regardless of which shebang was specified in other scripts, since those scripts are not the entrypoint of execution, but flask is.
Flask supports Python 3.7 and newer.
Setting Python 3 as default is what you're looking for I believe.
Running which python
shows that 2.7 is the default run ln -sf /usr/bin/python3 /usr/bin/python
to make Python 3 the default. That's the step I always take when setting up Flask on a fresh install of Ubuntu.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With