I can't import Flask - python

I'm trying to make an project with flask, but somehow, even if i installed it, i just cannot use flask!
i've tried to change the from Flask import Flask, render_template, request command by just import Flask, but nothing happen, could anyone help, please? Here is the code, if necessary:
import Flask
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')

It's
from flask import Flask
# 1st lowercase

The import is from flask import Flask.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')

Are you sure that you used the same Python version to install flask as you did to run your app? I once installed flask for Python 2.6, but executed the script in Python 3, which also led to the fact that the flask could not be imported. Be sure that the versions are correct.
sudo pip install Flask
sudo pip3 install Flask

Related

Flask will execute code in __init__.py but can't get it to run in app.py

If I understand the documentation correctly, in Python3 Flask ___init___.py is used for importing classes and can be completely empty.
I am trying to use app.py to hold my code, but I don't know how to get Flask to execute app.py. Code I put in __init__.py will execute, but I don't think I should do it that way. I have seen some posts where doing this could cause code to execute when it was just importing.
How do I get Flask to hit my app.py instead of __init__.py?
Here is my wsgi config file
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import app as application
application.secret_key = 'something secret'
In my wsgi.py I just import the app (from app.py) and run it. I do have an older setup though but the approach should still apply.
from app import app
if __name__ == "__main__":
app.run()
and my app.py has the global Flask app variable along with all the other setup and Flask code (logging, routes, etc.)
app = Flask(__name__)
Did you set the FLASK_APP environment variable to your app.py file?
$ export FLASK_APP=app
$ flask run
See: https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery

flask_socketio import wont work, error: No module named flask_socketio

Im trying to create connection between flask socketio and react native socketio, I have already prepared client side with react native socketio BUT I have run into problem with importing flask_socketio in rpi. Im trying to use simplest implementation possible, this is my code:
from flask import Flask
from flask_socketio import SocketIO, emit
app = Flask(__name__)
#app.route('/', methods=['GET'])
def hello_world():
return "Hello World"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5005)
Without line 2 it works perfectly but I need to use flask_socketio. here is photo too(first run is without importing flask_socketio, than I tried to import it and it wont work. I have tried reinstalling flask_socketio twice, rebooting but nothing works:/:
Problem was that I was installing it with sudo python pip install flask_socketio, BUT I had to use python3, so right installation is python3 -m pip install flask_socketio
You need to go to cmd prompt and use:
sudo -H pip3 install flask-socketio
This might resolve your problem
socketio = SocketIO(app, cors_allowed_origins="*")
this will work on any version of socketio

To Deploy my Python machine learning model as web service

I have developed a Machine learning model in anaconda prompt using Python3 so to deploy it and to run it anywhere dynamically on GitHub or as a webservice what is the way?
I have already used Pickle command but it didn't work for me.
import pickle
file_name = "cancerdetection.ipynb"
It didn't deploy my model as webservice.
You can try using flask. It uses python and deploys as a web application. Here's some sample code you can try.
from flask import Flask, render_template, request
app = Flask(__name__, static_url_path='/static')
#app.route('/')
def upload_file():
return "hello world"
if __name__ == '__main__':
app.run(host="0.0.0.0")
By setting
host="0.0.0.0"
you're deploying your application on the net by using your local machine as a server.

Flask CGI deploy on cPanel gives error 500

Stucked on little thing. I'm getting error 500, when running Flask app on cPanel.
[begaeuor#miami ~]$ python -V
Python 2.7.5
[begaeuor#miami ~]$
When I open python, I can import Flask package (Flask is installed system-wide).
Also I got installed, VirtualEnv, with Flask, but still no success.
[begaeuor#miami ~]$ which python
/usr/bin/python
[begaeuor#miami ~]$
Now. I get into cgi-bin, and created .cgi file:
#!/home/begaeuor/flaskdeploy/virtualenv-12.0.7/venv/bin/
from wsgiref.handlers import CGIHandler
from app import app
CGIHandler().run(app)
and also .py file:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return "Hello!"
if __name__ == '__main__':
app.run(debug=True)
Chmod is 0755 on both files.
Line endings is Unix (Sublime Text 3)
What am I missing?

Flask deployment on bluehost

I am facing problem while deploying flask application on bluehost server.
I made a newdomain.maindomain.com subdomain, and later did the following:
SSHed to the "newdomain" folder
created virtualenv and installed flask
created a file called "passenger_wsgi.py"
import sys,os
INTERP = os.path.join(os.environ['HOME'],'medsathome.freedatametric.com','bin','python')
if sys.executable != INTERP:
os.execl(INTERP,INTERP,*sys.argv)
sys.path.append(os.getcwd())
from flask import Flask
application = Flask(__name__)
#application.route('/')
def index():
return 'Hello Passenger'
now when I go to the link, I dont see Hello Passenger being printed on screen.
What is wrong?

Categories