Visual Studio Code: VS Code | ModuleNotFoundError: No module named '_crypt' - python

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

Related

error when running flask - no module with name '_crypt'

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.

How do I get the Flask package tutorial work with Poetry?

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.

Flask: ModuleNotFoundError: No module named 'SocketServer'

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
`

Start flask app from console - ModuleNotFoundError

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.

Basic Flask: ImportError: cannot import name Flask

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.

Categories

Resources