1

I'm trying to setup flask on an Amazon EC2 instance. I want to run flask with python 3.5 from an installed virtual environment.

However when I run flask it is using the ubuntu python 2.7 installation.

How do I tell flask to use the virtual environment python installation?

Thanks

Gavin Hinfey
  • 279
  • 1
  • 2
  • 13

2 Answers2

1

If you're running this from the CLI during development, you should already have python 3.5 on your server. If not then run sudo apt-get install python3. To run the app:

python3 app.py

However, since you are asking about virtualenv--which using is very good practice--you could create a new environment specifying the path of python3, as stated here. If you don't know the path, run which python3. For my computer, running Ubuntu 16.04:

virtualenv -p /usr/bin/python3 <path/to/new/virtualenv/>

Then:

source <env_name>/bin/activate

Now, this is not a very good setup even for dev and would be absolutely terrible for production. I would suggest using uWSGI and NGINX to serve your app. You can specify your virtualenv path in your uWSGI config--I would suggest using a .ini file. Here is a very good Digital Ocean tutorial that I used to get started. It won't teach you everything, but it should put you on the right path. There's a lot of resources for this configuration online, so you should be able to solve any problems you run into.

Community
  • 1
  • 1
Allie Fitter
  • 1,689
  • 14
  • 18
0

In app.py very first line add this:

#!/path/to/virtualenv/bin/python3.5

then chmod +x app.py

then you can run as:

./app.py

how to make python script self-executable

Community
  • 1
  • 1
oshaiken
  • 2,593
  • 1
  • 15
  • 25