me_ganesh
Posts: 23
Joined: Fri Mar 01, 2019 3:18 pm

No module named flask after installing flask

Fri May 24, 2019 9:22 am

hello!
I am currently working on Flask and uWSGI, honestly iam a complete beginner to flask.
I created a virtual environment in /var/www/lab_app/ named as 'venv'
I installed flask using the command

Code: Select all

sudo pip install flask
I created a python file in lab_app folder named hello.py which contains the following code

Code: Select all

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
        return "Hello World!"

if __name__ == "__main__":
        app.run(host='0.0.0.0',port=8080)
and when I try to execute the python file using

Code: Select all

python hello.py
it gives me the following error :
Attachments
rpiflask.png
rpiflask.png (47.41 KiB) Viewed 336 times
installed flask.png
installed flask.png (247.47 KiB) Viewed 336 times

User avatar
croston
Posts: 694
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: No module named flask after installing flask

Fri May 24, 2019 9:31 am

By using 'sudo' on the pip install command, you have installed Flask globally on your pi, not in your virtual environment. Just install it in your venv by not using sudo.

User avatar
DougieLawson
Posts: 35124
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: No module named flask after installing flask

Fri May 24, 2019 10:03 am

You've installed flask for python2 you need to use sudo pip3 install ... to get it for python3 (and all users).
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

me_ganesh
Posts: 23
Joined: Fri Mar 01, 2019 3:18 pm

Re: No module named flask after installing flask

Fri May 24, 2019 10:05 am

I uninstalled flask globally using

Code: Select all

sudo pip uninstall flask
and then in my virtual environment I tried to install flask using

Code: Select all

pip install flask


It gives me this error
Attachments
rasp-1-.png
rasp-1-.png (126.28 KiB) Viewed 319 times

me_ganesh
Posts: 23
Joined: Fri Mar 01, 2019 3:18 pm

Re: No module named flask after installing flask

Fri May 24, 2019 10:12 am

DougieLawson wrote:
Fri May 24, 2019 10:03 am
You've installed flask for python2 you need to use sudo pip3 install ... to get it for python3 (and all users).
I am using python 2

User avatar
DougieLawson
Posts: 35124
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: No module named flask after installing flask

Fri May 24, 2019 10:13 am

Your original errors have python3 written all over it. Stop using python2 for new code. Write everything new with python3.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

me_ganesh
Posts: 23
Joined: Fri Mar 01, 2019 3:18 pm

Re: No module named flask after installing flask

Fri May 24, 2019 10:40 am

sir, can you please explain this to me?

when I execute

Code: Select all

python --version
it gives out
Python 2.7.3

when I execute

Code: Select all

flask  --version
it gives out python version as 3.5.3
Attachments
rasp--.png
rasp--.png (40.04 KiB) Viewed 296 times

User avatar
rpdom
Posts: 14101
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: No module named flask after installing flask

Fri May 24, 2019 11:07 am

What do you get when you enter

Code: Select all

python3 --version
Signature is on holiday.

me_ganesh
Posts: 23
Joined: Fri Mar 01, 2019 3:18 pm

Re: No module named flask after installing flask

Fri May 24, 2019 11:15 am

how can there be two different versions of python?
I even deactivated my virtual environment
Attachments
rasp--.png
rasp--.png (33.93 KiB) Viewed 282 times

jojo777
Posts: 8
Joined: Mon Apr 16, 2018 11:29 am

Re: No module named flask after installing flask

Fri May 24, 2019 11:21 am

Code: Select all

sudo apt-get update
sudo apt-get install python3-pip
pip3 install --upgrade pip
pip3 install flask
python3 hello.py
Ave Cezar

Andyroo
Posts: 2833
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: No module named flask after installing flask

Fri May 24, 2019 11:55 am

me_ganesh wrote:
Fri May 24, 2019 11:15 am
how can there be two different versions of python?
I even deactivated my virtual environment
That is very normal (you should see some Macs). Both Python 2 and 3 are included by default on the Pi builds and modules get loaded into the release directory structure. e.g. V2 structure and it’s equivalent in your own projects at http://as.ynchrono.us/2007/12/filesyste ... ct_21.html. Note this can change depending on the platform you are on.

The command python is a link to the version 2 command so to access v3 you have to use python3 at the command line.

GUI editors (Thonny / Idle etc) can have different versions of Python used by default and Visual Code from MS lets you change versions from the editor.

If this sounds like a mess it is and trips lots of folk up lots of times BUT there is so much legacy code out there that removing Python2 is very hard to do.

From what I am seeing it’s best to either develop in Python 3 only (and use pip3 where needed) or live totally in a virtual environment where you can dictate how and where modules are loaded.
Need Pi spray - these things are breeding in my house...

me_ganesh
Posts: 23
Joined: Fri Mar 01, 2019 3:18 pm

Re: No module named flask after installing flask

Fri May 24, 2019 12:36 pm

Why am I not able to install any module using pip in my virtual environment?

Is it because I have created my virtual environment in /var/ directory?
Attachments
rasp--.png
rasp--.png (101.31 KiB) Viewed 243 times

Andyroo
Posts: 2833
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: No module named flask after installing flask

Fri May 24, 2019 12:51 pm

Could be or it could be that you used sudo to create the environment.

I’ve no Pi handy at the mo and feeling yuck so I cannot check for you :oops: but you could try changing the ownership of all objects / folders to pi:pi and see if that helps.

Not sure about using /var - I thought that was more system than user (but me new to bits of Linux :lol: ) http://www.linfo.org/var.html
Need Pi spray - these things are breeding in my house...

DirkS
Posts: 9804
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: No module named flask after installing flask

Fri May 24, 2019 4:54 pm

me_ganesh wrote:
Fri May 24, 2019 12:36 pm
Why am I not able to install any module using pip in my virtual environment?

Is it because I have created my virtual environment in /var/ directory?
who is the owner of /var/www ?
Often it's a specific user created for the web server and there could be a problem trying to access it with user 'pi'

Andyroo
Posts: 2833
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: No module named flask after installing flask

Fri May 24, 2019 5:03 pm

DirkS wrote:
Fri May 24, 2019 4:54 pm
...
who is the owner of /var/www ?
Often it's a specific user created for the web server and there could be a problem trying to access it with user 'pi'
Missed that - by default on every version of Raspbian I've used it has been www-data and not Pi.

Flask does not care about /var/www - it can serve pages from anywhere in the system if it has access to them and is covered here with pictures (OK - the daft robot and rocket at the top can be ignored)
Need Pi spray - these things are breeding in my house...

Return to “Beginners”