others-how to solve 'ModuleNotFoundError: No module named' error when using python ?

1. Purpose

In this post, I would demo how to solve the following ModuleNotFoundError when using python:

PyDev console: starting.

import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/Users/bswen/work/python/myutils'])

Python 3.7.6 (default, Jun 18 2020, 11:06:38) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
>>> import Flask
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'Flask'

2. The Environment

  • Python 3

3. Debug

3.1 The dependency installation verification

We have installed flask as follows:

➜  learn_flask git:(master) ✗ pip3 install flask         
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: flask in /Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages (1.1.2)
Requirement already satisfied: Werkzeug>=0.15 in /Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages (from flask) (1.0.1)
Requirement already satisfied: click>=5.1 in /Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages (from flask) (7.1.2)
Requirement already satisfied: itsdangerous>=0.24 in /Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages (from flask) (1.1.0)
Requirement already satisfied: Jinja2>=2.10.1 in /Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages (from flask) (2.11.2)
Requirement already satisfied: MarkupSafe>=0.23 in /Users/bswen/.pyenv/versions/3.7.6/lib/python3.7/site-packages (from Jinja2>=2.10.1->flask) (1.1.1)

3.2 The code that use the dependency

This is the python code that uses flask:

import Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

api.add_resource(HelloWorld, '/')

if __name__ == '__main__':
    app.run(debug=True)

4. The solution

4.1 The reason

Because Flask has upgraded, the old import style can not work!

We should change from:

import Flask

To:

from flask import Flask

4.2 The solution code

We should change our code as follows:

from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

api.add_resource(HelloWorld, '/')

if __name__ == '__main__':
    app.run(debug=True)

Now run it again, we get this:

/Users/bswen/.pyenv/versions/3.7.6/bin/python /Users/bswen/work/python/myutils/learn_flask/app1.py
 * Serving Flask app "app1" (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: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 717-204-539

It works!

6. Summary

In this post, we demonstrated how to solve the ‘ModuleNotFoundError: No module named xxx’ error in Python, we should check that the module was really installed correctly and you have correctly import the module. Thanks for your reading. Regards.

-->