ModuleNotFoundError: No module named 'pandas' in application service azure with flask - python

Help,
my application works normally on my device, but on azure the error occurs:
ModuleNotFoundError: No module named 'pandas'
The source:
from flask import Flask, request, render_template, session, redirect
import pandas as pd
app = Flask(__name__)
#app.route("/")
def inicio():
return render_template('index.html')
#app.route('/lista', methods=("POST", "GET"))
def html_table():
return render_template('lista.html')
if __name__ == "__main__":
app.run()
In browser returns:
:( Application Error
If you are the application administrator, you can access the diagnostic resources.
If I remove the panda it works normally. I'm still getting to know azure.

If you see an error like ModuleNotFoundError: No module named 'example', this means that Python could not find one or more of your modules when the application started. This most often occurs if you deploy your virtual environment with your code. Virtual environments are not portable, so a virtual environment should not be deployed with your application code.
Instead, let Oryx create a virtual environment and install your packages on the web app by creating an app setting, SCM_DO_BUILD_DURING_DEPLOYMENT, and setting it to 1. This will force Oryx to install your packages whenever you deploy to App Service.
This you have done this setting and got it
If you need more information, please refer please see this article on virtual environment portability, the SO thread and this.

Related

Flask environment not changing to development

Whenever running my Flask app on VSCode I am unable to change the environment to development. I have searched relevant links on this topic and tried all the solutions and combinations of solutions, nothing so far has worked. I would like my app to refresh whenever I make a change in either any of the template files or my app.py file. Here is a list of what I've tried:
Typing in "set FLASK_ENV=development" on my terminal from this stackoverflow solution I am on Windows, so I instead used set
Pip installing python-dotenv and creating a .env or .flaskenv. file inside my root directory (yes I tried both) then adding FLASK_ENV=development as well as set FLASK_ENV=development inside both files (at different times trying all combinations) like this stackoverflow solution mentioned
Editing my main flask app file with the lines mentioned in the solution from list item number two...
if __name__ == '__main__':
app.run(debug=True)
Yes I have read the documentation for python-dotenv here and added load_dotenv() after my import statements
See here start of my app.py code:
"""This is my controller"""
import re
from flask import Flask, render_template, request, redirect, session
from cs50 import SQL
from dotenv import load_dotenv
load_dotenv()
from flask_sqlalchemy import SQLAlchemy
# turn the current file into a web application that will listen for browsers requests
# __name__ refers to the current file
app = Flask(__name__)
if __name__ == '__main__':
app.run(debug=True)
And as you can see Environment: production still shows from terminal window
With powershell the syntax for env vars is different. To change the FLASK_ENV variable, type:
$env:FLASK_ENV = "development"
You can verify it with gci env: command that list all of the env vars. After that the flask run will run in development mode.
I discovered that for some reason Flask uses ENV variable instead of FLASK_ENV.
Try setting ENV=development in your .env file, it worked for me.

Unknown FastCGI error Occurred python web API

I am using python version 3.8 and IIS 7.0. When I try to host my python web api on the IIS server it encounter with the FastCGI error. I have enable CGI in IIS and also added System.WebServer>>handlers>>Python FastCGI in my web config but still it gives same error. I have also checked the wfastcgi and flask are also successfully added.
You had to put both parameters in double quote separated by |
e.g.
"c:\python39\python.exe" | "c:\python39\Lib\site-packages\wfastcgi.py"
or put the whole path in "" after pasting it.
follow below steps to configure iis flask app in iis:
1)install python
2)after installing python install the wfastcgi. run the command prompt as administrator and run below command:
pip install wfastcgi
wfastcgi-enable
3)below is my flask example:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello from FastCGI via IIS!"
if __name__ == "__main__":
app.run()
4)enable the cgi feature of iis:
5)open iis create a site.
6)after adding site select the site name and select the handler mapping feature from the middle pane.
Click “Add Module Mapping”
executable path value:
C:\Python37-32\python.exe|C:\Python37-32\Lib\site-packages\wfastcgi.py
Click “Request Restrictions”. Make sure “Invoke handler only if the request is mapped to:” checkbox is unchecked:
Click “Yes” here:
7)now go back and select the application setting feature.
click add from the action pane.
Set the PYTHONPATH variable(which is your site folder path):
And the WSGI_HANDLER (my Flask app is named app.py so the value is app.app — if yours is named site.py it would be site.app or similar):
8)Click OK and browse to your site.
Note: Do not forget to assign the iis_iusrs and iusr permission to the site folder and the python folder.

Deploying flask app on cpanel, INTERNAL SERVER ERROR when changing requesting other than base url

I want to deploy my flask-restx application on a shared hosting. Since I am beginner in deployment, I followed a video tutorial from youtube.
I did step by step by following this tutorial.
For those who do not want to go through the tutorial, I am writing the steps:
I created an application from the Python cPanel
Initial set up in Cpanel
Then I opened terminal and changed my venv and installed flask by "pip install flask"
Project Structure
filas_folder/
├──public
├──tmp
│ └──restart.txt
├──app.py
└──passenger_wsgi.py
app.py looks like
from flask import Flask
app = Flask(__name__)
#app.route("/")
def main_():
return "flask is running"
#app.route("/user")
def main_2():
return "user is running"
if __name__ == "__main__": app.run()
Restart app from cpanel
passenger.py looks like
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'app.py')
application = wsgi.app
when I open www.example.com
flask is running
But when I open www.example.com/user
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#example.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
My system has cloudlinux and uses apache server. This is not the first deployment. Many wordpress and static websites are running on the server.
I opened apache logs at /usr/local/apache/logs/error_log
I get the error "Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer" http://example.com/user"
Add the following to the top of your .htaccess file:
RewriteEngine on
RewriteRule ^http://%{HTTP_HOST}%{REQUEST_URI} [END,NE]
Got this info from: https://stackoverflow.com/a/63971427/10122266

Flask can't run in development mode, can't find flask module [duplicate]

This question already has an answer here:
Flask reloader crashes with "no module named Scripts\flask" on Windows
(1 answer)
Closed 3 years ago.
I'm on Windows, using git bash.
I'm able to run Flask if it's not in development mode. If I set FLASK_ENV to "development" to try to trigger debugging and other helpful things, then flask run fails.
The error message is pretty peculiar.
$ flask run
* Serving Flask app "Neuroethics_Behavioral_Task" (lazy loading)
* Environment: development
* Debug mode: on
* Restarting with stat
d:\miniconda3\python.exe: No module named D:\miniconda3\Scripts\flask
What's weird is that... in the directory D:\miniconda3\Scripts, there is a flask.exe or flask application file.
By typing "where flask", I get returned D:\miniconda3\Scripts\flask.exe. So this executable definitely exists in the location that they're trying to look for, but even then Flask can't seem to find it when I try doing flask run.
How do I approach this?
Edit: some more info. This seems to have to do with the fact that I had Flask previously installed in Anaconda, removed it from anaconda using conda uninstall flask, then I had it installed with pip.
where flask gives D:\miniconda3\Scripts\flask.exe
but pip show flask gives Location: d:\miniconda3\lib\site-packages. The location offered by pip however DOES NOT contain any flask.exe file whatsoever.
The flask run command is recommended, but there's currently an issue with Werkzeug on Windows. See Flask reloader crashes with "no module named Scripts\flask" on Windows for the workaround until the bug is fixed.
If you don't want to use that workaround, you can add an if __name__ == "__main__" block and call app.run(debug=True) directly, running it as python app.py. Again, this is not the recommended way to run Flask and there is another workaround to the bug.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)

Heroku Flask - Deploy a 'modular' app from tutorial not working, foreman start works locally

based on this structure: http://flask.pocoo.org/docs/patterns/packages/
I also tried this post: Deploying Flask app to Heroku
I am having trouble getting this to work on heroku. I usually get the PORT does not set within 60 seconds error. I have read other SO posts and just can't figure out if my project structure is wrong or my procfile. I tried other ports than 5000 as well.
Here is my current project structure:
/myapplication
Procfile
runserver.py
/applicationfolder
__init__.py
views.py
Here is my Procfile
web: python runserver.py $PORT
Here is my runserver.py
from applicationfolder import app
app.run()
if __name__ == '__main__':
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
Here is my init.py
import os
from flask import Flask
from flask import render_template, jsonify, request
app = Flask(__name__)
app.config.from_object('config')
import applicationfolder.views
From there views.py runs.
This works locally with foreman start and python runserver.py, but does not work with heroku. I have tried many things with PORT but port doesn't seem to set even with a different PORT than 5000. I think it has something to do with my project structure.
The app.run() was in there twice, which as you noted is what's screwing things up. The app.run() invokes a simply pure-python development server so that you can easily run and/or debug your script.
By invoking it at the module level (right under your import in runserver.py), you were effectively trying to start the development server as the python code was loaded, and then when it went to run it when invoked from the Procfile, the development server was already in flight, having been starting with it's defaults (latest version of Flask is pulling relevant defaults from the SERVER_NAME environment variable). By having it in both places, you were trying to invoke that method twice.
You basically want either the straight up module load (in which case, kill off the code under "if name ...", or you use the code when invoking under main, in which case don't start the service at module load time.

Categories