Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Flask use Python 3 by default

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.

like image 967
codeDojo Avatar asked Mar 05 '18 03:03

codeDojo


People also ask

How do I make Python 3 default to Python?

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.

Does flask use python3?

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.

What version of Python is flask using?

Flask supports Python 3.7 and newer.


1 Answers

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.

like image 194
BrettJ Avatar answered Oct 16 '22 06:10

BrettJ