ModuleNotFoundError: No module named 'flask', What did I do wrong? - python

I'm more or less new to computer science and can not even manage to run my first "Hello World" in Flask (Python 3; on my Windows-10 computer). I have tried some different tutorials on how to get started with Flask but did not find them really helpful so I did not finish them. Eventually I found a tutorial that seemed comprehensive and fitting my needs, but Visual Studio Code Terminal could not execute my code but gave me back an Error Message. Maybe I created a global virtual environment before I started this course that I'm following along now, and that's why it gives me that Error, but am not sure.
So this is the tutorial that I eventually picked and am currently following along: https://www.youtube.com/watch?v=QjtW-wnXlUY.
The tutorial showed me how to set up and activate a local virtual environment in my project folder and pip install Flask as well as save app.py, with the help of my command batch and VSCode.
Then the user should try and run the following code (see below this paragraph). On my browser it really worked perfectly fine, but the Visual Studio Code-Terminal gave me an error message called "ModuleNotFoundError: No module named 'flask'" (I know there are some similar-named threads out there in stackoverflow, but the answers given did not help me with my problem and also it seems to me that my case is slightly different). So this is the code I tried to run in Visual Studio Code:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello world!"
And this is the complete ErrorMessage that Visual Studio Code-Terminal gave me:
PS C:\Program Files\DB Browser for SQLite> & C:/Users/User/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/User/Desktop/Flask_app/app.py
Traceback (most recent call last):
File "c:/Users/User/Desktop/Flask_app/app.py", line 1, in <module>
from flask import Flask
**ModuleNotFoundError: No module named 'flask'**
PS C:\Program Files\DB Browser for SQLite>
Any ideas what's wrong with my code? Maybe it has to do with the path I stored it in? I got no idea; am very thankful for help!
Cheers.

Make sure the virtual environment you created is activated before running the app.
Also make sure it is activated when you run 'pip install flask'.

first install the flask in your system
pip install flask
run the flask server then try it
> set FLASK_APP=hello
> flask run

Related

Can't import flask-marshmallow into python3 project

I have only python 3.8.5 installed.
I started diving into Python and Flask for the first time (I'm coming from the javascript side of things). I've already ran into an interesting issue in just my first few lines of code.
I was able to run pipenv install flask flask-sqlalchemy flask-marshmallow marshmallow-sqlalchemy and they all seemed to have installed just fine. They all appear in the pipfile.lock. flask-marshmallow is version 0.13.0.
pipfile.lock
When I started coding, I was able to import flask and flask_sqlalchemy with no issues. Intellisense even helped me out with them. But from flask-marshmallow import Marshmallow didn't seem to work.
When I ran python app.py I get the following error
Traceback (most recent call last):
File "app.py", line 3, in <module>
from flask_marshmallow import Marshmallow
ModuleNotFoundError: No module named 'flask_marshmallow'
I've tried uninstalling flask-marshmallow and marshmallow and reinstalling. The console always says the installation is successful but I can't seem to import it when all the other packages seem to work fine.
What a great start to a new language xD Here is the whole file so far for reference, if I remove the marshmallow line it starts without any issues
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
app = Flask(__name__)
if __name__ == "__main__":
app.run(debug=True)
Make sure that, because you have created a pipenv, you have set the Python interpreter path correctly in your IDE.
I was facing the exact issue (and that is how I reached this question). I am using VS code, was using python3.8 and pipenv.
Even though I have installed the packages using pip3, I was facing import issues while running the code. After a futile search on the Internet, I realised that the issue was very silly.
The Python interpreter path (Cntrl+Shift+P -> Select Interpreter) was not set to the newly created pipenv. After I have set the interpreter path properly, the code resumed to function as expected.
i would suggest to check the site packages file and make sure that it is installed in it if not then it is installed in other directory
Also try working it in another normal python file and check if that works too
Check syntax also i know i know its obvious but just saying
In my case I was trying to run the command flask db init, and it would give the error above. I got it to work by running the command with sudo privileges sudo flask db init. I hope this can help someone else out in a similar situation!

Error Importing Requests Module in Flask Backend

I am currently trying to integrate a flask backend to my react web-app. In my flask subdirectory, I have installed requests module as shown below but I keep running into an unresolved import error although I have installed the module. I have tried to follow different troubleshooting suggestions online but none seem to work. Would really appreciate if someone can help me out.
This looks like that the terminal and the IDE use two different environments.
You should either create a virtual env with PyCharm directly, or choose the existing one.
Please read the PyCharm documentation:
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
There, either choose "New environment" or "Existing environment".

Pylint error: no name in module when import a package

I'm learning a little bit how function python, more specific Flask.
I'm creating a new project using flask, but when I create a new file and import Flask I receive this error: No name 'Flask' in module 'flask'pylint(no-name-in-module)
I did read that, I need create a file called: __init__.py but I have this and the error keeps showing up.
This is my actual code (very simple code, and folder/files structure):
Thanks for the help! :D
I tried this: Flask ImportError: No Module Named Flask, but this solution does not works for me.
I installed flask on python 2.7 and run simple hello world script and it worked fine. Then I updated python to python 3.8 and I got an error similar to yours:
Unable to import 'flask'pylint(import-error)
I simply uninstalled flask and installed again and it worked for me. Run this two commands:
pip uninstall flask
pip install flask
Then export the FLASK_APP environment variable and run flask
set FLASK_APP=file.py
flask run
I solved this changing my python interpreter. I did uninstall pylint and install pylama and now all is fine!

Zappa deployment error : GET request yields 502 response code

I am trying to deploy the first zappa example app built with Flask-Ask, It looks like everything works good but after the Deploying API statement I get the following error :
Error: Warning! Status check on the deployed lambda failed. A GET request to '/'
yielded a 502 response code.
Here is the code I am executing with minor changes to the sample app
from flask import Flask
from flask_ask import Ask, question, statement, session
import pyodbc
app = Flask(name)
ask = Ask(app, '/')
#ask.intent('HelloIntent')
def hello(firstname):
speech_text = "Hello %s" % firstname
return statement(speech_text).simple_card('Hello', speech_text)
#ask.intent('ByeIntent')
def bye():
return statement("Ok, goodBye!")
if name == 'main':
app.run()
Zappa version used: 0.46.1
Operating System and Python version: Windows 7, Python 3.6
Can someone please help me out here?
try installing all the dependencies using pip in the virtual environment where you are using zappa. It worked in my case.
You can also use zappa tail command to see your logs.
This github issue seems to have the same symptoms.
Downgrading to zappa==0.45.1 solved it for me
If you are using anaconda than create a new virtual environment "virtualenv lambda" in your project directory and in Scripts/activate. Than deactivate the conda environment with "conda deactivate" and pip install all the packages "pip install numpy pandas sklearn zappa flask".
PS: using "slim_handle"=true also gives this error, so don't use it.
If all of above doesn't work you can solve it by this way.
First solve all errors by checking the app log by zappa tail [app name], if you have any
Then,
You must provide "app_function" parameter in the zappa_settings.json which should point to your entry function. The app_function should be provided like this if application is Flask __init__.application, so the flask app should be defined as application as follows,
application = Flask(__name__)
app.py should be __init__.py
You have to add __init__.py in order to recognize your project folder as a package.
So zappa_settings.json have parameter like this,
"app_function": "__init__.application",
Deploy and enjoy!
I was facing this error when I gave the modular path to my application as main.py.
I fixed it by creating an empty file called main.app just next to main.py and setting app_function to main.app in zappa_settings.json.
Absolutely no clue what happened underneath, but it worked for me.
I had same problem. After spending couple of hours, from cloudwatch logs I noticed the error of sec certificate. Solved it by running "pip install 'cryptography<2.2'"
I faced the same error, and same what happened with ScottieB above, the reason was that I forgot to do the pip install for one package that my .app was using...After I did the pip install locally in the project environment then did the zappa update dev the error gone! and update been completed.
I had this same error and after many online searches and trying many, many suggestions, it was actually just a small problem with a code indentation! No problem with Zappa config or pip installations at all.
I notice that in your code sample you have not indented your code at all. I don't know if this is how it copy pasted into StackOverflow or if this is how you unintentionally tried to deploy it. It should be
#ask.intent('HelloIntent')
def hello(firstname):
speech_text = "Hello %s" % firstname
return statement(speech_text).simple_card('Hello', speech_text)
#ask.intent('ByeIntent')
def bye():
return statement("Ok, goodBye!")
if name == 'main':
app.run()
I was getting same error.
check you have installed zappa in your venv.
I have installed globally and run in local venv. When i installed zappa it works perfect.
I was facing the same issue and I figured out that zappa is installed globally on my system not in the virtual env that I am using.
try installing zappa in virtual env
pip install zappa
I was running a flask app and forgot to include following statement in my app.
if __name__ == '__main__': app.run()
after adding this line it started working fine.
👍

Using Flask and Jython under Windows 7

Hello everyone,
So I have been trying to use Jython to connect to an API Rest and retrieve some information. Now I want to use the Flask Framework with it. I have been trying to install the Flask with Jython but it does not seem to work at all. I am working on a Windows 7 machine and the problem for me is also that I can not download directly from the internet. For all other framework I used python wheels and installed these with Jython which worked fine.
I already tried to following commands and got these errors:
First error that I got was that it could not find the 'init.py' file in the flask folder so I changed the path in the file to the total path. But it just continued to give me more errors.
jython -m pip install '*.whl
Screenshot of the command line ouput of the error
pip install '*.whl (same as above)
I am a little stuck here and I hope that someone has an idea on how to solve this problem.
Big thanks already!!
This appears to be a bug with Jython 2.7.0. See this error report in pip and this one in Jython.
The second of those indicates that it is fixed in the 2.7.1 release candidate.

Categories