Getting a ModuleNotFound error with flask in python - python-3.x

I installed the flask module via following command.[![cmd image][1]][1]
When I ran the following code.
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
#a = input('Enter name')
return render_template(r'form.html')
if __name__ == "__main__":
app.run(debug=True)
It gave an error.
ModuleNotFoundError: No module named 'flask'
Note:- I am not using virtualenv. And I am having my html page in templates folder
[1]: https://i.stack.imgur.com/t4XzJ.png

It seems you have installed flask on the system wide environment. To access the flask in your virtualenv, you have to get into the virtualenv directory and activate it by using following command.
source virtual_env_name/bin/activate
replace 'virtual_env_name' with the virtualenv name you created.
After that in your command line, you will get (virtual_env_name) added before. the current directory path.
Now you can install the flask framework using pip3.
pip3 install flask
Keep in mind to activate venv everytime you are using the flask.

Related

how to fix flask import eror (there is no Flask in flask)

i installed flask in the cmd using the pip install flask command and i also tested it in the cmd and it works.
But when i go to pycharm all the modules i download using pip are for some reason in lib/site_packages
instead of just being in site_packages.the lib folder
and then i do "from flask import Flask"
and it says that " cannot import name 'Flask' from 'flask' (C:\Users\User\PycharmProjects\pythonProject1\flask.py)"
the error
there is no Flask in flask
Change the py file name to anything else. naming your flask file as flask.py will create a namespace conflict.

ImportError while running "Flask run app.py" but if I run "python3 app.py" then there is no import error and works fine

from flask import Flask, jsonify, request
from flask_restful import Api, Resource
from transformers import pipeline
#import transformers
app = Flask(__name__)
api = Api(app)
summarizer = pipeline("summarization", model='facebook/bart-large-cnn' )#t5-large
print("summarizer loaded")
class Summary(Resource):
def post(self):
# write what to do for post request and Add class
#Load the data
postedData = request.get_json()
#Validate the data
news = postedData['news']
summary_extractive = summarizer(news,min_length=90, max_length = 120)
#make json and return
retJSON = {
'Message': summary_extractive[0]['summary_text'],
'word_count': len(summary_extractive[0]['summary_text'].split()),
'Status Code': 200,
}
return jsonify(retJSON)
api.add_resource(Summary, '/get_summary')
if __name__ == '__main__':
app.run(debug=True)#host='0.0.0.0'
Error while running with flask run:
from transformers import pipeline
ImportError: cannot import name 'pipeline' from 'transformers' (unknown location)
But if I run with python3 app.py then there is no error.
I'm working in venv enviroment on macOS.
In pip list I'm able find the missing module transformer.
I have faced similar issue which is causes due to old version of pip. Follow the below step to resolved the issue:
upgrade your pip version use this command pip install --upgrade pip
Uninstall old version of transformer pip uninstall transformers
install again pip install transformers
It works have a nice day

flask_sqlalchemy module cant be found error

So I want to use the flask_sqlalchemy module in my web app. But when I run "flask run" it gives me the following error:
from flask_sqlalchemy import
SQLAlchemy
ModuleNotFoundError: No module
named 'flask_sqlalchemy'
I am running this in a virtual env with python3.7 and pip3. It says that all packages have been installed. Ive tried a whole bunch of things with no luck.
So after trying a whole bunch of stuff. Setting the environment variable FLASK_ENV=development and then to production somehow fixed.

ModuleNotFoundError: No module named 'flask_wtf' in python

I'm new to python so please be patient if i ask a silly question.
I am trying to use flask wtf class as seen in a tutorial.
My first line of the program is
from flask_wtf import FlaskForm
It gives me the error ModuleNotFoundError: No module named 'flask_wtf'
I have followed the following steps to make sure i have activated the virtual environment and flask wtf was installed in the virtual environment and not global.
Step 1: Went to the virtual environment and activated the environment C:\Users\Shraddha\PycharmProjects\FlaskProject\venv\scripts activate
Step 2: Installed flask wtf
C:\Users\Shraddha\PycharmProjects\FlaskProject\venv\scripts pip install flask-wtf
I see that flask-wtf is installed correctly in the folder C:\Users\Shraddha\PycharmProjects\FlaskProject\venv\Lib\site-packages
However when i try to run it using command prompt it does not give me anything and when i try to run it using sublime text it gives me ModuleNotFoundError: No module named 'flask_wtf' error.
I know this is a flask_wtf import error because other programs are executing as expected. Any help ?
If you start your virtual environment in the same dir as the file you are running doesn't that give access to the libraries in the install (ie everything shown in 'pip freeze' for the env)?
I faced same issue and tried all the approaches given on stackoverflow ImportError: No module named flask_wtf but didn't work for me.
I am using ubuntu 20.04 LTS operating system.
Note: To follow the given step you must activate your virtual environment.
I used:
pip3 freeze
which listed following directories
libries in my virtual environment
Then i tried the below command in the same directory
git clone git://github.com/lepture/flask-wtf.git
cd flask-wtf/
python3 setup.py install
Through the above commands i successfully installed flask_wtf as show below
installed flask_wtf
but then i faced the below error as i used email_validator in my forms.py file:
Exception: Install 'email_validator' for email validation support
Then I installed email_validator using following command
pip3 install email_validator
Then i successfully run my flask application. I am sure it will work for you too.

windows 10 python modulenotfounderror

I have an issue with Python 3.6.3 on Windows 10.
I have installed flask_bootstrap. Here is my (edited) pip freeze:
C:\Users\pablo\willwriter2>pip freeze
Flask==0.12.2
Flask-Bootstrap==3.3.7.1
When I run a very simple script:
from flask import Flask
from bootstrap import Bootstrap
app = Flask(__name__)
bootstrap = Bootstrap(app)
#app.routes('/')
def homepage():
return "<h1>Hello World</h1>"
I get the following error message:
File "C:\Users\pablo\willwriter2\main2.py", line 2, in <module>
from bootstrap import Bootstrap
ModuleNotFoundError: No module named 'bootstrap'
This error occurs whether I use a virtual environment or the global python install. It has occurred for other modules as well.
Does anyone know why this is happening?
Thanks,
Paul.

Resources