ModuleNotFoundError: No module named 'flask'


#1

I have gone through and created a Python3 virtualenv per the Python Sample config (https://docs.aws.amazon.com/cloud9/latest/user-guide/sample-python.html):

virtualenv --version
which python
unalias python
python --version
python3 --version
which python36
cd ~/environment/
virtualenv -p /usr/bin/python36 vpy36
source vpy36/bin/activate
python --version
deactivate
alias python=/usr/bin/python27

I then pip install flask successfully.

When I try to run a sample hello world flask app I am presented with the following error:Traceback (most recent call last):
File “/home/ec2-user/environment/flask_init/hello.py”, line 3, in
from flask import Flask, request, render_template
ModuleNotFoundError: No module named ‘flask’

I am able to run from the cli, but that does not enable me to be able to use the debugger.

I would appreciate any assistance on this.


#2

Hello,

How to

On your runner panel / window (the one with the green arrow run button ):

  • select the Runner: “Python 3”
  • Then click on “Edit Runner”, this will open a “Python 3.run” editor.
  • In this editor, at that beginning of line 6 and 8 replace python3.6 with the full path to your virtualenv’ python intertpreter (which should be /home/ec2-user/environment/vpy36/bin/python in your case)
  • Save your runner giving it a name like “vpy36.run” (don’t omit the .run extension)

Select the runner, you should be able to run or debug now.

why

source …/vpy36/bin/activate is just some sugar to put …/vpy36/bin/python in your path

The runner (and the debugger) need to use the proper python interpreter. That’s why you modify the runner make it point to the correct python.

Cyril