Flask on Windows powershell [duplicate] - python

This question already has answers here:
How to run a flask application?
(6 answers)
Closed 9 months ago.
i have installed flask and i am trying to run flask using the windows power shell
but i am unable to do so
PS C:\path\to\app> $env:FLASK_APP = "hello.py"
the format is given the documentation what should we type in $env?
p.s. I was able to run flask using command prompt using the code

first, you have to activate the virtual environment for your Flask app (i'm assuming you are using venv the default virtual environment module for python : py -m venv venv)
PS C:\myapps\flask\helloflask> .\venv\Scripts\activate
(venv) PS C:\myapps\flask\helloflask>
set the FLASK_APP environment variable:
(venv) PS C:\myapps\flask\helloflask> $env:FLASK_APP="helloflask:create_app('development')"
note the double quotes wrapping the app name "helloflask:create_app('development')" otherwise Power Shell triggers a red error.
refer to this doc on using app factory pattern and i would recommend you this good reads on How to Run a Flask Application
maybe you want to check if FLASK_APP has been set:
(venv) PS C:\myapps\flask\helloflask> $env:FLASK_APP
helloflask:create_app('development')
now you can run your Flask app:
(venv) PS C:\myapps\flask\helloflask> flask run
* Serving Flask app "helloflask:create_app('development')"
* Environment: development
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [23/Jun/2020 12:47:20] "←[37mGET / HTTP/1.1←[0m" 200 -
finally i recommend you using .flaskenv under the root of your project to store your Flask app related environment variables (FLASK_APP among others):
/.flaskenv
FLASK_APP=helloflask:create_app('development')
# FLASK_APP=helloflask:create_app('testing')
# FLASK_APP=helloflask:create_app('production')
FLASK_ENV=development
FLASK_DEBUG=0
# FLASK_RUN_EXTRA_FILES=
# FLASK_RUN_HOST=
# FLASK_RUN_PORT=8080
# FLASK_RUN_CERT=
# FLASK_RUN_KEY=
To take advantage of this option you have to install the python-dotenv package (have a look at this doc). To do so :
(venv) PS C:\mypps\flask\helloflask> pip install python-dotenv

I don't know if this will still be usefull but this is how I got mine to work.
In powershell type:
.venv/Scripts/activate and press Enter
In the comand line make sure you see something like: (.env) if so then type:
$env:FLASK_APP = "yourappname.py" and press Enter then type
$env:FLASK_ENV = "development" and finally type
flask run

Related

conda and flask not logging anything

I just started with python and am confused that my code does not behave the way the examples show.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'Server Works!'
#app.route('/greet')
def say_hello():
return 'Hello from Server'
conda run -n myenv flask run
after this command, nothing appears, yet the app works.
Using VSCODE and Windows 10 Terminal
The reason you don't see anything in the console is because conda run capture your output.
If you want to run the above command with conda run, you need to use the folowing command
conda run --no-capture-output flask run
But I think you shouldn't run command like that. Activate the environment first and do anything in activated shell.
conda activate -n myenv
flask run

ModuleNotFoundError: No module named 'hello'

I did something really bad. I don't know what I did. I created a test project with hello.py where I did some mistake when running with some command. Now, I have deleted that and back to the real project, and I got the following error:
File "/home/bhojendra/anaconda3/envs/myenv/lib/python3.9/site-packages/flask/cli.py", line 240, in locate_app
import(module_name)
ModuleNotFoundError: No module named 'hello'
I don't have even the word hello anywhere in the project.
I have removed all pycaches from the project, cleaned the conda conda clean -a, removed myenv environment and removed pip cache directory. Then, I re-created the environment and and re-installed the requirements and when launching the project with flask run, it throws that error again. Not sure what's happening.
It might be the issue with flask cache, I don't know how to overcome this issue.
In your environment, you likely left your FLASK_APP set to the file hello, so it was trying to run that, although it doesn't exist. You just have to export or set your flask app based on what machine you are using.
Unix Bash (Linux, Mac, etc.):
$ export FLASK_APP=hello
$ flask run
Windows CMD:
> set FLASK_APP=hello
> flask run
Windows PowerShell:
> $env:FLASK_APP = "hello"
> flask run
You could also unset the export:
unset FLASK_APP
And then set the flask app

makefile to start Flask in debug mode

I would like to start Flask in debug/development mode using a single command.
Given
A fresh terminal, changing into the project directory, activating a virtual environment and running a custom Makefile:
> cd project
> activate myenv
(myenv) > make
Output
Debug mode is OFF. However, running the commands separately turns it ON (as expected):
(myenv) > set FLASK_APP=app.py
(myenv) > set FLASK_ENV=development
(myenv) > flask run
Output
Code
I've created the following Makefile, but when run, the debug mode does not turn on:
Makefile
all:
make env && \
make debug && \
flask run
env:
set FLASK_APP=app.py
debug:
set FLASK_ENV=development
How do I improve the Makefile to run Flask in debug mode?
Note: instructions vary slightly for each operating system; at the moment, I am testing this in a Windows command prompt.
While I still believe a Makefile is a more general approach on other systems, I settled with #user657267's recommendation to use a batch file on Windows:
Code
# start_flask.bat
:: set environment varibles (app and debug mode)
set FLASK_APP=app.py
set FLASK_ENV=development
flask run
pause
Demo
> start_flask.bat
Output
set FLASK_APP=app.py
set FLASK_ENV=development
flask run
* Serving Flask app "app.py" (lazy loading)
* Environment: development
* Debug mode: on
* Restarting with stat
* Debugger is active!
* ...
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
I am willing accept another solution.
Makefiles should be deterministic. Having one command which could toggle between the two is not the best way to do it.
Simply create your makefile like so:
FLASK_APP = app.py
FLASK := FLASK_APP=$(FLASK_APP) env/bin/flask
.PHONY: run
run:
FLASK_ENV=development $(FLASK) run
.PHONY: run-production
run-production:
FLASK_ENV=production $(FLASK) run
Now you can just do
make run
or
make run-production
Alternatively, on Linux inside of a makefile you can place everything on a single line without export keyword.
debug:
FLASK_APP=app.py FLASK_ENV=development flask run
As per flask docs.

Ansible - Start Python Flask Script in New Terminal Window

I have a Flask app that I'd like to deploy and start using Ansible. I've already got my playbook setup to install all the dependencies needed but I'm having trouble getting Ansible to start my application. I've used command and shell both of which do startup the Flask app, but they block Ansible from exiting and Flask doesn't popup in it's own terminal window, which makes it difficult to visually debug and see what Flask is doing.
#Current playbook; both command and shell act the same way
tasks:
- command: python3 /home/user/Desktop/Flask/app.py
- shell: python3 /home/user/Desktop/Flask/app.py
Q: How can I have Ansible startup a Flask script in it's own terminal window?
If you have gnu screen installed on this system then you can use it to background tasks. I use this to run a shell script asynchronously as the deploy user so that I can log in later and see how it's doing:
- name: Invoke reset script
command: /usr/bin/screen -d -m sudo -u deploy /usr/local/bin/do-reset.sh -i -y reset
async: True
poll: 0
The -d -m parameters tell screen to start up in detached mode, and the async and poll settings tell Ansible to background the command and forget about it.

How do I run a Python script that is part of an application I uploaded in an AWS SSH session?

I'm trying to run a Python script I've uploaded as part of my AWS Elastic Beanstalk application from my development machine, but can't figure out how to. I believe I've located the script correctly, but when I attempt to run it under SSH, I get an import error.
For example, I have a Flask-Migrate migration script as part of my application (pretty much the same as the example in the documentation), but after successfully SSHing to my EB instance with
> eb ssh
and locating the script with
$ sudo find / -name migrate.py
when I run in the directory (/opt/python/current) where I located it with
$ python migrate.py db upgrade
at the SSH prompt I get
Traceback (most recent call last):
File "db_migrate.py", line 15, in <module>
from flask.ext.script import Manager
ImportError: No module named flask.ext.script
even though my requirements.txt (present along with the rest of my files in the same directory) has flask-script==2.0.5.
On Heroku I can accomplish all of this in two steps with
> heroku run bash
$ python migrate.py db upgrade
Is there equivalent functionality on AWS? How do I run a Python script that is part of an application I uploaded in an AWS SSH session? Perhaps I'm missing a step to set up the environment in which the code runs?
To migrate your database the best is to use container_commands, they are commands that will run every time you deploy your application. There is a good example in the EBS documentation (Step 6) :
container_commands:
01_syncdb:
command: "django-admin.py syncdb --noinput"
leader_only: true
The reason why you're getting an ImportError is because EBS installs your packages in a virtualenv. Before running arbitrary scripts in your application in SSH, first change to the directory containing your (latest) code with
cd /opt/python/current
and then activate the virtualenv
source /opt/python/run/venv/bin/activate
and set the environment variables (that your script probably expects)
source /opt/python/current/env

Categories

Resources