I am running the following code
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
and getting the following error
Traceback (most recent call last):
File "test.py", line 1, in <module>
from flask import Flask
File "/home/pi/programs/flask.py", line 1, in <module>
from flask import Flask
ImportError: cannot import name Flask
I have tried installing flask through various methods , but still the problem persists
also, is there any alternative to flask???
I ran into this error because I named the test file as flask.py and tried to run it! It creates namespace conflict with the real flask module!
Delete the local test file that you named flask.py and the respective flask.pyc. Give some other name! This will happen with other modules like socket etc where you are likely to give the same name for the test file as the standard module :-)
The reason is your python file name is flask.
Just run apt-get install python3-flask
Edited to install the python3 version, since nobody should be using python2 now.
Just rename flask.py file also delete flask.pyc file
It's because of the name flask.py. It will import itself if the name is flask.py. Change the name and try again.
Restart virtual environment
$ virtualenv flask
Into dir flask run
$source ./bin/activate
Install python module again
$pip install "module"
I had the same issue. Apparently you can't name your file socket.py either.
exactly when we create file name as flask.py and then execute it for the first time it will execute and at the same time framework creates another file called flask.pyc .Now stop the process and start it again it will throw this error as instead of looking to actual framework flask file it is looking in to the one you created . To solve this problem
Go to the folder you created flask.py and delete flask.pyc and then rename flask.py to some test_1.py ..save it and execute it . You should see no errors .
Add PYTHONPATH to your environment:
export PYTHONPATH=/root/environments/my_env/lib/python3.6/site-packages/
Related
I am a beginner with the python programming.
I have python 3 installed in my local system.
I coding along as part of a tutorial video and as part of the tutorial, i have created a virtual environment and created an app.py file with the below content.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello, World!"
if __name__ == "__main__":
app.run()
I have installed all the dependencies like flask and pytest in the virtual environment as per the tutorial using gitbash.But when i run the command python3 app.py in gitbash i get the below error message
File "C:\path\Python\python-github-actions-example\src\app.py", line 1, in <module>
from flask import Flask
ModuleNotFoundError: No module named 'flask'
(myvenv)
I checked the python version and it is python 3.9.7
If i run python app.py i get the output.
Why is it not running even though correct version is installed
Any idea why ?
Try to delete the venv or make a new one.
Then create a new venv like this:
virtualenv flask
Go to the flask directory
: cd flask
Activate it: scripts\activate
You should see (flask) on the left of the command line.
Install flask again: pip install flask
Run your file again.
Some times you need to change/select de versiĆ³n/env if you after installed. like it if work for you.
After you do what #kmogi says in their post above, start the app with python not python3.
I'm new to flask framework and I want to write a simple flask app that uses another python module (librosa package). I have successfully installed librosa in the same virtual environment that I have installed flask and I can easily import it in the python interpreter. Here is the python script.
# app.py
from flask import Flask
import librosa
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello'
if __name__ == '__main__':
app.run()
The problem is that when I want to run the flask app using the flask run command (after setting export FLASK_APP=app.py) I get the ModuleNotFoundError error as follows:
Error: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "/home/masoud/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/masoud/projects/my_projects/seperator_rewriten/app.py", line 2, in <module>
import librosa
ModuleNotFoundError: No module named 'librosa'
The strange thing is that there is no import error when I run the flask server using python app.py command.
if you are using an IDE such as pycharm, then you may need to install it from the terminal in the IDE itself, not cmd
This may be the solution to your problem:
python -m flask run
The reason is that flask run may use the python executable somewhere else, not the virtual environment you created for the project.
You can add these lines of code in your script:
import sys
import flask
print(sys.executable)
print(flask.__version__)
And then you can check the python executable path using python app.py and flask run.
The problem
I start a flask app, which works fine when I use "run" or "debug." However, I got the error when use "profile," here is the log message
In folder C:/Users/Myname/PycharmProjects/ProjectName
"C:\Users\Myname\Anaconda3\envs\ProjectName\python.exe" "C:\Program Files\JetBrains\PyCharm
Professional Edition with Anaconda plugin 2019.2.4\helpers\profiler\run_profiler.py" 127.0.0.1 12055 -m flask run
Starting cProfile profiler
Traceback (most recent call last):
Snapshot saved to C:\Users\Myname/.PyCharm2019.2/system\snapshots\ProjectName1.pstat
File "C:\Program Files\JetBrains\PyCharm Professional Edition with Anaconda plugin 2019.2.4\helpers\profiler\run_profiler.py", line 173, in <module>
profiler.run(file)
File "C:\Program Files\JetBrains\PyCharm Professional Edition with Anaconda plugin 2019.2.4\helpers\profiler\run_profiler.py", line 89, in run
execfile(file, globals, globals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Professional Edition with Anaconda plugin 2019.2.4\helpers\profiler\prof_util.py", line 30, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:\Users\Myname\Anaconda3\envs\ProjectName\lib\site-packages\flask\__init__.py", line 19, in <module>
from . import json
ImportError: attempted relative import with no known parent package
What I have tried
I google it and found that it is an old problem.
Can't profile module using relative imports
Pycharm profile is not working
How to reproduce it
If you would like to reproduce it, you can create a flask app using pycharm, and then click "profile."
The default code for flask app is as follows. But I don't think it matters.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
The error message can be different due to the change of the flask version or Pycharm version, but the reason is the same: File "xxx\lib\site-packages\flask\__init__.py", line 19, in <module> use relative import and cause the ImportError
My question
My question is, is there a way to make it work? Any hacks or tricks are also appreciated. I just want to use the profile now. Thank you
Apparently this is a long-standing issue with PyCharm's profiler, it does not run packages correctly resulting in sys.path being wonky and relative imports failing: https://youtrack.jetbrains.com/issue/PY-28509
I guess an option would be to make your own "launcher script" simply copying what flask.__main__ does.
Though things might also work out if you run -m flask.cli instead of just -m flask (I don't know, I didn't test it): it does the same thing but does not invoke a __main__ file so it could behave better.
[New solution]
Thanks to #Masklinn, use -m flask.cli instread of -m flask could solve this problem. However, the configuration in pycharm flask use -m flask by default and I cannot find out a way to modify it. Therefore, I suggest to use pycharm python. The script path should be set to your FLASK_APP, and add -m flask.cli in addtional options.
[Obsolete solution]
One solution is that directly modify the __init__.py to make it work. For example, change
from . import json
from .app import Flask
to
from flask import json
from flask.app import Flask
My Flask Application is crashing when I'm trying to access it.
This is similar error to this or this. However, my set-up seems correct.
flask.cli.NoAppException: The file/path provided (server) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py
My environment variable correctly set.
export FLASK_APP=server.py
export FLASK_DEBUG=1
My server file is server.py, and I don't have any __init__.py in the directory.
I don't recall having change anything special in the code. Where does the bug could come from?
The issue was that some package were missing or corrupted.
I reinstalled everything with pip3 install -r requirements.txt --ignore-installed and now it works fine.
This error may be a sign that an import is not found.
To see which import is missing, try to run your server.py file with the python interpreter:
python yourapp.py
Example of output :
Traceback (most recent call last):
File "yourapp.py", line 4, in <module>
from flask_httpauth import HTTPBasicAuth
ImportError: No module named flask_httpauth
Source (and other leads)
For me, solution was fixing a misspelled function name "create_app" in my _ init _.py
from flask import Flask
def create_app(test_config=None):
app = Flask(__name__)
return app
Super late answer, but I just ran into this and the solution was to use a Remote WSL connection in VS Code. I thought I was in the correct folder, but VS Code was looking at a very similar folder in Windows. You can tell you're in the wrong folder by running "touch somefile.abc" in your linux terminal, from the project folder, but that file doesn't automatically appear in VS Code's folder structure.
https://code.visualstudio.com/docs/setup/linux
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl
I'm working through Miguel Grinberg's Flask Mega-Tutorial, and am unable to run the basic app in Part I. I am using Ubuntu, and getting the following traceback:
Traceback (most recent call last):
File "./run.py", line 2, in <module>
from app import app
File "/home/makisupa43/dev/microblog/app/__init__.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
I have double checked code and all looks correct. Not sure if I'm getting screwed up with VirtualEnv or if it is a separate issue.
Reading through the tutorial, it looks like Miguel skips the step where you actually activate the virtual environment. Run this command from the directory where you're doing all your pip installs:
. flask/bin/activate
This should put you in the correct virtual environment, which will make everything you've installed with pip available to your run.py script.