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
`
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 have a problem running flask. It started fine until I loaded a simple login form.
when running flask via gitbash, I get an error like this
flask run
* 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]
Try 'flask run --help' for help.
Error: While importing 'app', an ImportError was raised:
Traceback (most recent call last):
File "C:\Users\****\AppData\Local\Programs\Python\Python39\lib\crypt.py", line 6, in <module>
import _crypt
ModuleNotFoundError: No module named '_crypt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\****\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\cli.py", line 234, in locate_app
__import__(module_name)
File "E:\Program Files\xampp\htdocs\****\app.py", line 2, in <module>
from crypt import methods
File "C:\Users\****\AppData\Local\Programs\Python\Python39\lib\crypt.py", line 9, in <module>
raise ImportError("The crypt module is not supported on Windows")
ImportError: The crypt module is not supported on Windows
btw, i used windows
remove this from the top of the file:
from crypt import methods
it should work again. At least it did for me. VSCode may have autogenerated this for you.
This thing occur most times with auto-import in vs code.
If you inspect your code critically, you'd notice that your vs-code editor has automatically included the line of code from crypt import methods above your imports.
Just comment or delete it out and try re-running your app again, you'd be in good shape.
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.
I'm a fairly new programmer I installed flask through pip and was trying to run it in Pycharm, it wouldn't work, so I downloaded Flask 1.02 on Pycharm, it then gave me some error messages which I will include in some pictures. This has been driving me crazy, if anyone can help me fix this I would truly appreciate it!
Error message!
Edit these are the error messages:
Traceback (most recent call last):
File "/Users/Farhan/PycharmProjects/Helloworld/fuck_you.py", line 1, in <module>
from flask import Flask
File "/Users/Farhan/PycharmProjects/Helloworld/venv/lib/python3.6/site-packages/flask/__init__.py", line 17, in <module>
from werkzeug.exceptions import abort
File "/Users/Farhan/PycharmProjects/Helloworld/venv/lib/python3.6/site-packages/werkzeug/__init__.py", line 151, in <module>
__import__('werkzeug.exceptions')
File "/Users/Farhan/PycharmProjects/Helloworld/venv/lib/python3.6/site-packages/werkzeug/exceptions.py", line 67, in <module>
from werkzeug._internal import _get_environ
File "/Users/Farhan/PycharmProjects/Helloworld/venv/lib/python3.6/site-packages/werkzeug/_internal.py", line 30, in <module>
string.digits +
AttributeError: module 'string' has no attribute 'ascii_letters'
I found an answer! So, I previously tried to install flask on the virtualenv on pycharm before, but this time I created a new file and a new project within that file, and reinstalled flask on Pycharm with pip install flask, and surprisingly it worked.
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.