Start flask app from console - ModuleNotFoundError - python

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.

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.

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
`

Flask - ModuleNotFoundError, though it is in the same folder - Updated

everyone. Can't understand where my problem is.
My structure is:
But in my cmd I have:
(venv2) C:\Users\chainsaw\Desktop\work\bot-1\archie\autosales_bot>flask run
* Environment: production
WARNING: This is a development server. Do not use it in a production deployme
nt.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: While importing "autosales_bot.wsgi", an ImportError was raised:
Traceback (most recent call last):
File "c:\users\chainsaw\appdata\local\programs\python\python37-32\lib\site-pac
kages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "C:\Users\chainsaw\Desktop\work\bot-1\archie\autosales_bot\__init__.py",
line 1, in <module>
from admin_panel.run import generate_password
ModuleNotFoundError: No module named 'admin_panel'
Thanks in advance
Maybe I should run it as
python run.py
?
I have init.py in my apps

Heroku Moduke Not Found Error

[![][1]][1]
the module 'pyicloud' is listed in requirements.txt
I have checked using the 'ls' command- I checked the directory in which the package should be - and the package does exist on the heroku server.
The app gives no such issues on my local machine, even though there is no difference in the code
I also tried the suggestions given here : https://help.heroku.com/BWJ7QYTF/why-am-i-seeing-importerror-no-module-named-site-when-deploying-a-python-app
but it didnt work.
(venv)dhcp-10-4-0-197:mes-amis mananhora$ heroku run python manage.py db upgrade -a mesamis
▸ heroku-cli: update available from 6.16.18-62346b1 to 6.99.0-ec9edad
Running python manage.py db upgrade on ⬢ mesamis... up, run.1121 (Free)
Traceback (most recent call last):
File "manage.py", line 5, in <module>
from project import app, db
File "/app/project/__init__.py", line 40, in <module>
from project.import_data.functions import import_data_blueprint
File "/app/project/import_data/functions.py", line 6, in <module>
from pyicloud import PyiCloudService
ModuleNotFoundError: No module named 'pyicloud'

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