cannot set flask environment variable in macos - python

After typing
export FLASK_APP=use_flask.py
And then typing
printenv
which outputs inter alia:
TERM_SESSION_ID=AA805368-CF19-4631-AABB-A0112AD535CE
FLASK_APP=/users/me/documents/pcode/color/use_flask.py
USER=me
CONDA_EXE=/Users/me/Applications/miniconda3/bin/conda
I then place in the file use_flask.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return 'hey man'
And then type
ls
which outputs inter alia
use_flask.py
I then type:
flask run
I get the error
-bash: flask: command not found
I have also tried exporting the full path of the use_flask.py file but that did not work either. What am I doing wrong?

I ran
pip install flask flask-alchemy
I thought I already had flask installed because when I typed
from flask import Flask
into my Pycharm editor and then ran the code no error message occurred. So although I solved the problem I don't understand why it didn't work the first time.

Related

How do I fix #app.route syntax error with Flask?

from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello from Flask!'
if __name__ == "__main__":
app.run()
But Python is prompting the following error:
#app.route('/')
...
File "<stdin>", line 2
^
SyntaxError: invalid syntax
I ran your code and it worked for me. There does not seem to be a clear syntax error.
Try saving your file again and clearing your terminal before running it.
Another alternative is running the following in your command prompt before running your code:
set FLASK_APP={name of your file}.py
In the command prompt it should look something like this:
C:\path\to\app>set FLASK_APP=hello.py
Lastly, you can also try running it by writing the following in your command prompt or terminal instead of using the main() function you have now:
python -m flask run
More details and alternatives here: https://flask.palletsprojects.com/en/1.1.x/quickstart/
Hope this helps!

"Fatal error in launcher:" when using pip or flask

When I try to install anything with Pip, it gives me the "Fatal error in launcher:" error. That wouldn't be too bad, since I know how to update Pip differently. However, the same error occurs when I try to run the "flask run" command.
I'm using Windows 10, Python 3.8.2 and I have previously set the FLASK_APP variable to flaskblog.py. This is its content:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
Since you haven't posted the error clearly, if Fatal error in launcher: Unable to create process using ‘”‘ is your error,
then you might want to update pip using python -m pip install --upgrade pip and try installing your package again using python -m pip install <pkg-name>. You should get it working.
if the error persists after the above mentioned steps, the try importing pip in your python console(pull up your terminal and type python then type import pip) and try pip.main([‘install’,’<pkg-name>’]) in the console.
Hope this helps.
referred from here
Edit
Alternatively, you can run your flask app by adding app.run() in your script.
Like:
if __name__ == __main__:
app.run()
and then in the terminal, run python -m flaskblog.py.
Note: if you want to run your app in debug mode, consder giving debug = True to app.run().

Flask tutorial : cannot execute .run.py. terminal doenst recognize shebang?

I am following the tutorial here:
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
I have also create a
app/view.py
from app import app
#app.route('/')
#app.route('/index')
def index():
return "Hello World!"
and
app/init.py
from flask import Flask
app = Flask(__name__)
from app import views
I am down to the last step and written the
run.py
file as such:
#!flask/bin/python
from app import app
app.run(debug=True)
I am running it by evoking the ./run.py command in terminal. getting the following error:
from: can't read /var/mail/app
./run.py: line 4: syntax error near unexpected token `debug=True'
./run.py: line 4: `app.run(debug=True)'
I am running a conda virtual environment.
All the answers I see online suggest adding the shebang but its already there. I will appreciate guidance on this.
That's not a valid shebang - it has to be an absolute pathname, in other words the first character after the #! can only be a slash.

Is there any option getting ipython command prompt while running flask

Let me try to explain my problem with example now.
Here is sample GUI code with Tkinter
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
If I run this code in Ipython, I don't get a command prompt when the GUI is visible.
Now if I comment out the line, "root.mainloop()", the code still works in Ipython and I have access to command prompt so that I can inspect data when the code is running.
Now coming to the Flask case,
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
app.run()
When I run this application in Ipython, I don't get a command prompt. To access variables while the code is running, I need to stop the flask server.
Is there any option to run the flask server and have access to command prompt?
Thank you
I second #NewWorld and would recommend a debugger. You can inspect the program in an IPython shell with the IPython debugger. Install e.g. with:
pip install ipdb
Then load the debugger with: ipdb.set_trace() like;
#app.route('/')
def hello_world():
import ipdb; ipdb.set_trace()
return 'Hello World!'
This will open a IPython command prompt and you can inspect "data while the code is running".
Further information:
Look here to get started with ipdb.
This site gives a short introduction to available commands once inside the debugger.
run the flask application in separate thread.
try this example:-
from flask import Flask
import thread
data = 'foo'
app = Flask(__name__)
#app.route("/")
def main():
return data
def flaskThread():
app.run()
if __name__ == "__main__":
thread.start_new_thread(flaskThread,())
run this file in ipython:-
"run -i filename.py"
then you can access the ipython terminal for inspection.
If you want to use flask shell through ipython you can install the following package:
pip install flask-shell-ipython

flaskr tutorial; can't import flaskr (initialize database)

I'm new to programming, and tried to work through the flask tutorial.
http://flask.pocoo.org/docs/tutorial/
I'm stuck on this part (from the readme on github) when trying to run the app:
https://github.com/mitsuhiko/flask/tree/master/examples/flaskr/
Fire up a python shell and run this:
from flaskr import init_db; init_db()
I get this error when I try to run the command in python shell:
Import error: No module named flaskr
And I get this error when I try to run app locally:
sqlite3.OperationalError
OperationalError: unable to open database file
I've been looking for solution for several hours now, but to no avail.
Any thoughts on what I could check? Thank you.
The thing that fixed it for me was changing
export FLASK_APP=flaskr
to
export FLASK_APP=flaskr.py
Taken from here
The simplest way to accomplish what you need is to fire up the Python shell in the same folder as you have flaskr:
# I'm assuming that python is available on the command line
$ cd path/to/flaskr
$ python
# Python then runs and you can import flaskr
>>> from flaskr import init_db; init_db()
>>> exit()
The trick is that when you run Python it only looks in a certain number of places for modules and packages - you can see which places by running:
>>> from sys import path
>>> for fp in path:
... print fp
from the Python interpreter. If the path to flaskr is not in that list flaskr can't be imported. By default Python adds the directory it is started in to its search path (which is why we start Python in the directory that contains flaskr.)
Once you have run init_db you should be able to run the application and see everything working.
If you're using a version of Flask < 0.11, the flask command is not available. Install the flask-cli package in that case.
pip install flask-cli
To anyone else who finds this, add init_db() to the main executer to the end of your flaksr app as follows:
if __name__ == '__main__':
init_db()
app.run()
That should solve the sqlite error and stop you from having to run init_db() manually.
When we say :
export FLASK_APP=flaskr
by no means python understands where the package "flaskr.py" exists.
One of the way to solve the issue is choosing the right path where the "flaskr.py" resides. For eg, change your current working directory to where the file exists and :
export PYTHONPATH=`pwd`
Then you can execute "flask run" anywhere you want.
PS: flask tutorial seems broken. :)
Sean Viera's answer was very good, although I'd like to add that I was encountering the same problem and want to add to the solution. Running Python from the same flaskr folder was not enough for me. It was also necessary to activate Flask before running $Python by running the ". venv/bin/activate" command, like so:
$ cd path/to/flaskr
#active
$ . venv/bin/activate
(venv)$ python
# Python then runs and you can import flaskr
>>> from flaskr import init_db;
>>> init_db()
>>> exit()
$
Hope that extra bit of info helps!
$set FLASK_APP=flaskr
$python -m flask initdb
$python -m flask run
Try this:
OS: Windows
(venv)$pip install -I --no-deps Flask
(venv)$set FLASK_APP=flaskr
(venv)$set FLASK_DEBUG=1
(venv)$flask run
Explain:
$where flask will help to locate flask.exe
if virtual-env inherits flask from system-env
(venv)$where flask
>> /system/environment/path/to/flask.exe
(root)$where flask
>> /system/environment/path/to/flask.exe
obviously it calls flask.exe which is installed for system-env
(venv)$pip install -I Flask can force to (re)install flask for virtual-env
(venv)$pip install -I Flask
(venv)$where flask
>> /virtual/environment/path/to/flask.exe
>> /system/environment/path/to/flask.exe
I have same problem, and I have fixed it by.
step1:
sudo ln -sf /usr/bin/python3.4 /usr/bin/python
easy_install_3.4 flask.
PYTHONPATH=pwd
step2: using easy_install_3.4 to install falsk.
step3:
Heading

Categories

Resources