How to Fix ImportError: No module named 'flask' with virtualenv

Flask is one of the most used frameworks in Python. If you are new to the flask and if you are getting this error - ImportError: No module named 'flask' on virtualenv, read further.

You are sure that the Flask module is already installed. But still, you are getting No module error. This is because you need to install a flask framework inside the virtualenv.

Follow these instructions


Step 1

Create a directory where you will be installing the virtualenv.

 

krishna@osboxes:~$ mkdir testenv

 

Step 2

Get inside the folder and install virtualenv

krishna@osboxes:~$ cd testenv/

krishna@osboxes:~/testenv$ pip install virtualenv

or

$ sudo apt-get install virtualenv

 

Successfully installed appdirs contextlib2 distlib filelock importlib-metadata importlib-resources pathlib2 scandir singledispatch six typing unknown unknown virtualenv


Step 3

Activate the virtualenv

This virtual environment requires activation and dedicated installation inside the virtual environment. It has to be installed after creating a virtual environment. The libraries will reside inside the folder created for the virtual environment.

 

krishna@osboxes:/tmp/tempenv$ source tempenv/bin/activate

(tempenv) krishna@osboxes:/tmp/tempenv$

 

Please note that you are inside the virtual environment.

Step 4

Install Flask inside the virtual environment

(tempenv) krishna@osboxes:/tmp/tempenv$ pip install Flask

Successfully installed Flask-1.1.2 Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 itsdangerous-1.1.0

Make sure that there is Flask package available inside the virtual env folder

 

(tempenv) krishna@osboxes:/tmp/tempenv$ ls -ltr tempenv/lib/python3.5/site-packages/ | grep -i flask

drwxrwxr-x 4 krishna krishna 4096 Aug 22 11:05 flask

drwxrwxr-x 2 krishna krishna 4096 Aug 22 11:05 Flask-1.1.2.dist-info

 

Step 6

Create a generic test.py basic python script to test the Flask

 

from flask import Flask

app = Flask(__name__)

@app.route('/')

def hello_world():

    return 'Hello World!'

if __name__ == '__main__':

    app.run()

 

Execute the python script

(tempenv) krishna@osboxes:/tmp/tempenv$ python test.py

 * Serving Flask app "test" (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://127.0.0.1:5000/ (Press CTRL+C to quit)


4 comments:

  1. com/wp-content/uploads/2021/06/Screen-Shot-2021-06-01-at-1 Liquid synthetic urine will come with a concentrated liquid base, while the powdered version will come with dehydrated urine powder that is mixed with water to create a urine-like consistency It is also critical to avoid all skin-to-skin contact with anyone who has consumed cannabis It can cause nausea and upset stomach in some cases Just make sure to notify your doctor because certain detox practices can be dangerous during this time  Support local journalism by subscribing to your Blank Slate Media community newspaper for just $35 a year Visit: https://www.urineworld.com/

    ReplyDelete

Ultimate Python interview questions for Intermediate Level Python Programmer.

Question : What is metaprogramming in Python?  Answer : Writing programs that treat other programs as data, using metaclasses, decorators, a...