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
Related
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
I have this script:
import os
secret = os.environ.get('MFS')
print(secret)
For some reason, it returns None. I set that environment variable to a secret key that I want to use to set app.config['SECRET_KEY']. But, if I run this command: python3 -c 'import os;print(os.environ.get("MFS"))', it returns the correct environment variable. It doesn't matter if I cd to the project folder and run that or not, it just works. Even though I am using a venv to run the Flask app, that command still works when I run the venv Python. Why is this happening?
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
I would like to make a Flask API running into docker with a conda environment.
It seems that I can install the conda environment from the .yml file.
But I can't run the app when I do docker run.
I just have errors about files that do not exist
exec source activate flask_env && python app.py failed: No such file or directory
The flask API is based on a simple example:
from flask import Flask, jsonify
app = Flask(__name__)
#app.route('/', methods=['GET'])
def hello_world():
return jsonify({'message': 'Hello World'})
#app.route('/test', methods=['GET'])
def test():
return jsonify({'test': 'test'})
if __name__ == "__main__":
app.run(debug=True) # remember to set debug to False
The Dockerfile is:
FROM continuumio/miniconda3:latest
WORKDIR /app
# Install myapp requirements
COPY environment.yml /app/environment.yml
RUN conda config --add channels conda-forge \
&& conda env create -n myapp -f environment.yml \
&& rm -rf /opt/conda/pkgs/*
# Copy all files after to avoid rebuild the conda env each time
COPY . /app/
# activate the myapp environment
ENV PATH /opt/conda/envs/myapp/bin:$PATH
# Launch the API
CMD [ "source activate flask_env && python app.py" ]
And the environment file is:
name: myapp
channels:
- conda-forge
- defaults
dependencies:
- flask=1.0.2
- python=3.7.3
I tried a lot of thing but I can't make it works. Did I miss something ?
Thank you
See this:
The CMD instruction has three forms:
CMD ["executable","param1","param2"] (exec form, this is the preferred form)
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (shell form)
Here, you CMD is used as parameters of ENTRYPOINT(see this), so you have to use next format:
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
But, your command have && in it, so you have to enable shell in json format. So for your case, this could be next, FYI:
CMD ["bash", "-c", "source activate flask_env && python app.py"]
I want to run IPython interactively while running my Flask app. I tried ipython -i app.py but I don't get a prompt until after the app exits. I want to do this so I can debug each step of the program. How can I run my app and be able to examine it?
The -i flag runs the given program and then puts you in an interactive session after it has run. There is no way to debug the program using this flag.
Instead, you want to use a debugger. python -m pdb app.py will start pdb, a console debugger. There are other debuggers available, such as the graphical one built into IDEs such as PyCharm and PyDev, or more advanced console ones such as pudb.
Implemented with thread
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,())
And open the IPython Command prompt and type the command run -i filename.py