Python ImportError: No module named flask

If you are getting “ImportError: No module named flask” while running python flask hello world application, then follow this tutorial to resolve the error.

sneppets@cloudshell:~/mypythonproj (sneppets-gcp)$ python app.py
********************************************************************************
Python command will soon point to Python v3.7.3.

Python 2 will be sunsetting on January 1, 2020.
See http://https://www.python.org/doc/sunset-python-2/

Until then, you can continue using Python 2 at /usr/bin/python2, but soon
/usr/bin/python symlink will point to /usr/local/bin/python3.

To suppress this warning, create an empty ~/.cloudshell/no-python-warning file.
The command will automatically proceed in  seconds or on any key.
********************************************************************************
Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from flask import Flask
ImportError: No module named flask

Check flask installed or not

Whenever you get error “ImportError: No module named flask” first check whether flask is installed or not. The easiest way to check is go to ‘python terminal’ or ‘command prompt’ and try the following command >>> import flask

sneppets@cloudshell:~/mypythonproj(sneppets-gcp)$ python
********************************************************************************
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> import flask

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named flask

The above results clearly shows that flask module in not installed.

Create Virtual Environment

Note, different python applications uses packages and modules that’s not included in standard library. Also sometimes applications requires specific version of a library.

To meet the above requirement python supports virtual environments and packages. Virtual environment is a self contained directory that contains python installation for specific version of python and other supporting packages and libraries. Different applications can use different virtual environments.

The module used to create and manage virtual environments is called as venv. To install python3-venv  run the following command.

$ sudo apt install python3-venv
The following additional packages will be installed:
  python3.5-venv
The following NEW packages will be installed:
  python3-venv python3.5-venv
0 upgraded, 2 newly installed, 0 to remove and 17 not upgraded.
---------------------------------------------
---------------------------------------------

To create new virtual environment run the following command

$ sudo python3 -m venv venv
$ ls
app.py venv

The above command creates a directory called venv , which contains a copy of python binary, pip package manager and other supporting files needed.

$ cd venv/
$ ls
bin  include  lib  lib64  pyvenv.cfg

You need to activate the virtual environment in order to use it. So activate the virtual environment using the following command. Once activated, you would see shell prompt will change and it will show the name of the virtual environment that you are currently using. In our case it is venv.

$ source venv/bin/activate
(venv) $

Install Flask to resolve Flask ImportError

In the above steps we activated the virtual environment, now use Python package manager pip to install Flask as shown below.

(venv) $ sudo pip install flask

Collecting flask
  Downloading Flask-1.1.1-py2.py3-none-any.whl (94 kB)
     |████████████████████████████████| 94 kB 2.8 MB/s
Requirement already satisfied: Werkzeug>=0.15 in /usr/local/lib/python2.7/dist-packages (from flask) (0.16.1)
Requirement already satisfied: click>=5.1 in /usr/local/lib/python2.7/dist-packages (from flask) (7.0)
Collecting itsdangerous>=0.24
  Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting Jinja2>=2.10.1
  Downloading Jinja2-2.11.0-py2.py3-none-any.whl (126 kB)
     |████████████████████████████████| 126 kB 19.4 MB/s
Collecting MarkupSafe>=0.23
  Downloading MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl (24 kB)
Installing collected packages: itsdangerous, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.11.0 MarkupSafe-1.1.1 flask-1.1.1 itsdangerous-1.1.0

Verify the installation

To verify the installation try running the following command to check the Flask version.

(venv) $ sudo python -m flask --version

Python 2.7.13
Flask 1.1.1
Werkzeug 0.16.1

Now try running the minimal flask application that you had created. It should run without any errors now.

(venv) $ sudo python app.py

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

Deactivate virtual environment

Once you are done with running your application, you can deactivate the virtual environment by typing the command deactivate to return back to normal shell

(venv)$ deactivate
$

Further Learning

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments