I'm following along with a Flask tutorial, and I'm creating a run.py file, running chmod a+x run.py, and running the file as ./run.py.
Unfortunately, I get this:
Traceback (most recent call last):
File "./run.py", line 3, in <module>
from app import app
File "/Users/benjaminclayman/Desktop/microblog/app/__init__.py", line 1, in <module>
from flask import Flask
ImportError: cannot import name Flask
For reference, my run.py file is:
#!flask/bin/python
from app import app
app.run(debug=True)
And when I run
from flask import Flask
there's no issue (I don't get any error message).
I looked at similar issues on SO and it looks like often it was having a file called flask.py but I don't have one (AFAIK).
Any idea what I did incorrectly?
Make your shebang
#!/usr/bin/env python
and run again. See this question.
Related
I was making a simple test with python Flask in windows. After the base code:
from flask import Flask, jsonify
Flask(__name__)
app = Flask(__name__)
#app.route('/', methods=['GET'])
def ping():
return jsonify({"response": "funcionando"})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=4000, debug=True)
I try to run it from terminal:
python app.py
and this error comes across:
import _crypt
ModuleNotFoundError: No module named '_crypt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\src\app.py", line 1, in <module>
from crypt import methods
That package is not supported on windows, therefore it will fail to run the api.
After some research, I discover that VS Code add this line at the top:
from crypt import methods
Just delete this line (or comment it), save it and run it again.
Now everything will work
I'm using Poetry to manage my Python project (dependencies, package, etc) and I'm trying to implement the four-line hello world from Flask documentation: https://flask.palletsprojects.com/en/1.1.x/patterns/packages/#simple-packages
My folder structure looks like this:
myproject/
.venv/
myproject/
__init__.py
views.py
poetry.lock
pyproject.toml
README.rst
The two files, init.py:
import myproject.views
__version__ = '0.1.0'
from flask import Flask
app = Flask(__name__)
And views.py:
from myproject import app
#app.route('/')
def index():
return 'hello world'
Yet when I run export FLASK_APP=myproject and FLASK_ENV=development and do flask run, and point my browser to the port, it says
flask.cli.NoAppException
flask.cli.NoAppException: While importing "myproject", an ImportError was raised:
Traceback (most recent call last):
File "/myproject/.venv/lib/python3.9/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/myproject/myproject/__init__.py", line 1, in <module>
import myproject.views
File "/myproject/myproject/views.py", line 1, in <module>
from myproject import app
ImportError: cannot import name 'app' from partially initialized module 'myproject' (most likely due to a circular import) (/myproject/myproject/__init__.py)
How can I fix this? I'd like to use the recommended pattern.
In your terminal, type poetry run flask run.
I believe your issue comes from importing the views at the top of __init__.py. Try moving the import statement to the end of the file, just like the tutorial does.
I have a strange error, that I never saw before.
I wrote a small http web server using flask and when I run I got the error bellow.
I am using virtual env and my python version is 3.8
What can be?
from flask import Flask, request
app = Flask(__name__)
#app.route("/")
def index():
print(request.get_json())
if __name__ == "__main__":
app.run(debug=True)
python tools/fake.pyTraceback (most recent call last):
File "/home/ubuntu/workspace/otc/venv/lib/python3.8/site-packages/werkzeug/serving.py", line 58, in <module>
from http.server import BaseHTTPRequestHandler
ModuleNotFoundError: No module named 'http.server'; 'http' is not a package
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tools/fake.py", line 1, in <module>
from flask import Flask, request
File "/home/ubuntu/workspace/otc/venv/lib/python3.8/site-packages/flask/__init__.py", line 16, in <module>
from werkzeug.exceptions import abort
File "/home/ubuntu/workspace/otc/venv/lib/python3.8/site-packages/werkzeug/__init__.py", line 15, in <module>
from .serving import run_simple
File "/home/ubuntu/workspace/otc/venv/lib/python3.8/site-packages/werkzeug/serving.py", line 61, in <module>
import SocketServer as socketserver
ModuleNotFoundError: No module named 'SocketServer'```
The issue comes from the first traceback:
ModuleNotFoundError: No module named 'http.server'; 'http' is not a package
You need to rename the http.py file to something else because it is overriding the standard library http module. To fix it, you will need to do the following:
Rename the file http.py to something else.
Remove .pyc files in the project
find . -name "*.pyc" -delete
Run the program again
Python3 uses socketserver, all lowercase. Python2 uses SocketServer
this maybe helps you
`
pip uninstall Flask
pip uninstall Werkzeug
pip install Flask
pip install Werkzeug
`
Traceback (most recent call last):
File "manage.py", line 6, in <module>
from app import create_app, db
File "C:\Users\miran\Dropbox\PyReddit-master\app\__init__.py", line 6, in <module>
from instance.config import app_config
ModuleNotFoundError: No module named 'instance'
I am testing out this repository (https://github.com/tuvtran/PyReddit) and it keeps telling me there is no module instance, I have searched but found nothing that helped the error. The venv is in the same folder as the script.
Essentially this is occurring because the app\__init__.py is is importing a file in the instance directory. As per the Flask documentation, this directory normally contains instance configuration overrides default configurations.
Unfortunately, the project currently saves its default configurations in this directory, but it is not checked into Git (see .gitignore). To fix this:
Remove from instance.config import app_config
Add a configuration file following these instructions
Import the configuration file you just created
Replace app.config.from_object(app_config[config_name]) with app.config.from_object(your_configuration)
Why, when I try to run a flask application from the console, I encounter an error: ModuleNotFoundError:
$ python run.py
Traceback (most recent call last):
File "run.py", line 1, in <module>
from src.app.controller import app
ModuleNotFoundError: No module named 'src'
or
$ env FLASK_APP=run.py flask run
* Serving Flask app "run.py"
* 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
Usage: flask run [OPTIONS]
Error: While importing "run", an ImportError was raised:
Traceback (most recent call last):
File "/home/pronect/PycharmProjects/it-informer-master/venv/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/pronect/PycharmProjects/it-informer-master/src/run.py", line 1, in <module>
from src.app.controller import app
ModuleNotFoundError: No module named 'src'
Here is a project screenshot:
But if I run the application from Paycharm, then everything works fine.
How to solve this problem?
Just try to change the code to this:
from app.controller import app
from config import Config
The file run.py is already in the directory src. The error occurs, because there is no other folder called src in the directory where the file run.py is stored.