Cannot find an existing dependency when running a python module from Flask - python

I have a python module, it works fine, it uses pandas as one of its dependencies.
I added a Flask application in the middle with a rest service which has a service that imports and runs the very same module.
module = importlib.import_module('modules.' + script.module, package=__package__)
Both the python module and the flask app run with the same virtualenv activated, in which pandas is installed.
-BUT- when running this through the flask app, it throws an exception:
ImportError: No module named pandas
Tried to run the module without calling it from Flask and it works just fine...
=== UPDATE ===
I don't know why Flask is running on python 2 even though I'm running it with a virtualenv of python 3 activated.
This is the script I'm using to start my Flask app:
#!/bin/bash
export FLASK_APP=run.py
export FLASK_DEBUG=1
flask run
I'd appreciate any help
Thanks a lot

I've fixed it.
I haven't installed Flask within the virtualenv I was running, so I think that's why Flask was running on a different python version.
So I installed flask on my virtualenv and now it works.

Related

Can't run the module flask in python

I've been trying to run a code from the internet but I get this error that says:
in
from flask import Flask
ModuleNotFoundError: No module named 'flask'
This is supposed to be a referrence to something i need to work with and i don't know how to fix this as it's not my code.
Here's the link to the code where i got it from:
https://www.geeksforgeeks.org/create-simple-blockchain-using-python/
I just need to run it once so that i could get working on my stuff too.
Edit: i use the basic idle from python btw...
Maybe you dont have flask installed in your dev environment. Do
pip install Flask
or
pip3 install Flask
to install flask

ModuleNotFoundError: No module named 'icalendar' google Cloud

I'm getting "ModuleNotFoundError: No module named 'icalendar' " after running google app deploy on Gcloud, I have installed the icalendar module using pip but to my surprise i'm getting an error when I try to deploy the app. I have spent hours on this your help will be much appreciated.
According to the official documentation :
Specifying Dependencies
Dependencies for Python applications are declared in a standard
requirements.txt
For example:
Flask==0.10.1
icalendar
Therefore I created a requirements.txt and included icalendar module. Then I deploy to App Engine gcloud app deploy and everything worked as expected.
You can follow this tutorial for better understanding the concept:
Quickstart for Python 3 in the App Engine Standard Environment
Pls. check pip list to see if that has been installed properly. If yes, pls. check that import statement in the python terminal.
If that works, then use which python and ensure that it is the same virtual environment where you are installing and running from.

python flask code - on windows through HTTP request

I have come with python flask code which uses python libraries pywin32 and pYAD. I need to run these in production enviornment.
I tried to enable it over IIS through wfastcgi but its not working as expected.
Is there a way I can leverage the script to use in production set up, I have tried using Always Up converting script as a service but its not allowed in my organization similar with NSSM.
Is there any other way to explore this. Kindly help
Thanks
You can try waitress. You'll find the documentation Here.
Install Waitress
pip install waitress
setup.py file
from waitress import serve
import main
serve(main.app, host='0.0.0.0', port=8080) #'main.app' being your entry point
run setup.py and access your app through http://host_ip_address:8080

What is the difference between using flask run vs python app.py vs python -m flask run? [duplicate]

This question already has answers here:
How to run a flask application?
(6 answers)
Closed 3 years ago.
The following ways allows me to launch the Flask server.
Option 1:
set FLASK_APP = app.py
flask run
Option 2:
set FLASK_APP = app.py
python -m flask run
Option 3:
python app.py
What is the difference between using either of this?
$ python app.py
This is the simplest, standard way of calling the Python interpreter to run any Python script. It is not specific to Flask. The app.py may or may not have a if __name__ == "__main__" block (see What does if __name__ == "__main__": do?), but if you are going to do this for Flask, it is required to have __main__ method that calls app.run().
From the Flask docs:
The alternative way to start the application is through the
Flask.run() method. This will immediately launch a local server
exactly the same way the flask script does.
Example:
if __name__ == '__main__':
app.run()
The docs also state why even though this works, it is not recommended:
This works well for the common case but it does not work well for
development which is why from Flask 0.11 onwards the flask method is
recommended. The reason for this is that due to how the reload
mechanism works there are some bizarre side-effects (like executing
certain code twice, sometimes crashing without message or dying when a
syntax or import error happens).
This way is also problematic if you need to modify run configurations (ex. port) depending on the host environment. For example, you need to use port 5500 instead of the default 5000 when running on a certain machine. You can of course do this with os.environ and app.run(port=5500), but it's going to be "messy" modifying the code based on environment-related configs that are unrelated to the code.
That is why we have the second way, the flask command line tool.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
$ set FLASK_APP=app.py
$ flask run --port=5500
You can now maintain your code to be independent of any external environment configurations. Aside from that, the flask CLI tool has a lot of other options for configuration and debugging, such as enabling/disabling DEBUG mode, listing routes (flask routes), and getting env vars from .env files.
Notice also that your app does not have to explicitly call app.run and __name__ now isn't going to be __main__. This is helpful for cases where your app is just part of a bigger package and/or it needs to be run from some other directory. See the Larger Applications section of the Flask docs.
Finally, we have the third way:
$ python -m flask run
This is another standard way of running Python scripts. It is also not specific to Flask. From the docs:
When called with -m module-name, the given module is located on the
Python module path and executed as a script.
This means flask will be searched from the invoked python module search path. This is particularly useful when your environment has multiple versions of Python and you want to make sure you are using the correct Python version and env with Flask. It can also be useful when you have multiple Flask installations for multiple projects. It explicitly sets which Python interpreter to use to call the flask CLI tool.
$ python3.8 -m flask --version
Python 3.8.10
Flask 1.1.2
Werkzeug 1.0.1
$ python3.8 -m flask run
$ python3.7 -m flask --version
Python 3.7.4
Flask 1.1.1
Werkzeug 0.16.0
$ python3.7 -m flask run
$ python -m flask --version
Python 2.7.16
Flask 1.0.3
Werkzeug 0.14.1
$ python -m flask run
flask run
This one looks for an executable (called flask) on your PATH, the first one executes with a parameter run, which will make the flask helper run the application by calling FLASK_APP.
python -m flask run
This one looks for an executable called python on your PATH, the first one executes receiving -m as argument, which is supposed to run a module (flask) and then pass the parameter run to it.
The key difference here, is that as it executes the first found executable on PATH, you can run a totally different Flask from the first one. You can also run a different python version's Flask.
python app.py
This calls the first python executable on PATH, and passes app.py as argument. It will make python run the app.py script, which may or may not have app.run() (This is what starts Flask).
If you don't have anything inside app.py, it won't call Flask's server.

Can't import Flask while using Google App Engine

I'm following this guide and trying to develop a Flask app to run on the Google App Engine. I followed the guide to the letter but when I launch the dev app server from the Launcher and go to http://localhost:8080/, I get a HTTP 500 error.
I check the logs and it says No module named flask. Then I check the interactive console in the admin console by running import flask and I get the same error message. I can import flask in any other python file without error.
Is there a way to fix this?
Working a bit with GAE and Flask I have realized this:
Running directly with Python
To run the app with python directly (python app.py) you need have the dependents packages installed in your environment using the command: pip install flask
Running with dev_appserver.py
To run the app with the dev_appserver.py provided by GAE SDK you need have all dependent packages inside your project, as: Flask, jinja2... Look in my another answer a example how to configure this packages : https://stackoverflow.com/a/14248647/1050818
UPDATED
Running Python, Virtualenv, Flask and GAE on Windows
Install Python
Install Python http://www.python.org/ftp/python/2.7.2/python-2.7.2.msi
Click in Windows Start button and search by "Edit the system environment" and open
Go to the tab Advanced and click on button "Environment Variables…"
When the Environment Variables window opens, choose Path from the System variables list and click Edit…
Add this ;C:\Python27;C:\Python27\Scripts at the end of the value and save
Install setuptools MS Windows installer (Necessary to install PIP on Windows)
Choose the correct installer for you in this page http://pypi.python.org/pypi/setuptools#files( I used this one: http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11.win32-py2.7.exe#md5=57e1e64f6b7c7f1d2eddfc9746bbaf20)
Download the installar and Install that
Install PIP
Download PIP http://pypi.python.org/pypi/pip#downloads
Extract it to any folder
From that directory, type python setup.py install
Install Virtualenv
Execute pip install virtualenv
Execute this mkdir c:\virtualenvs to create a folder to the Virtual Envs
Execute this cd c:\virtualenvs to access that folder
Execute virtualenv flaskdemo to create a virtualenv for you project
Active the virtualenv c:\virtualenvs\flaskdemo\scripts\activate
Install Google App Engine SDK
Install the SDK https://developers.google.com/appengine/downloads
Create the project
Create a directory for your project
Create the main of your application https://github.com/maxcnunes/flaskgaedemo/blob/master/main.py
Create the configuration of your appliction for Google App Engine https://github.com/maxcnunes/flaskgaedemo/blob/master/app.yaml
Create a file to let GAE initialize your application https://github.com/maxcnunes/flaskgaedemo/blob/master/initialize_gae.py
(Look a example of the code here: https://github.com/maxcnunes/flaskgaedemo )
Install Flask to run Locally
Execute pip install flask
Install Flask to run on the GAE
Download Flask https://github.com/mitsuhiko/flask/archive/0.9.zip and extract the folder flask inside your project
Download Werkzeug https://github.com/mitsuhiko/werkzeug/archive/0.8.3.zip and extract the folder werkzeug inside your project
Download Jinja2 https://github.com/mitsuhiko/jinja2/archive/2.6.zip and extract the folder jinja2 inside your project
Download Simple Json https://github.com/simplejson/simplejson/archive/v3.0.5.zip and extract the folder simplejson inside your project
Running the application with GAE SDK
Open Google App Engine Launcher
Add a new application
Run the application
Click in Browse button to open your application on browser
Finally click on Deploy button to deploy your application
Usually, templates come with a requirements.txt. If not, add your dependencies there and then run pip install -t lib -r requirements.txt to force the libraries to be saved in the lib folder.
Make sure you've added lib to appengine_config.py with vendor.add('lib') if it's not already there.
I was also facing the same issue and after spending 1 day on it have found out my silly mistake actually while refactoring my flask app I have changed
appengine_config.py to some other name.
Ideally appengine_config.py should look like below if you are having all your dependencies in lib folder only
from google.appengine.ext import vendor
#Add any libraries installed in the "lib" folder.
vendor.add('lib')
And because it was not able to find and execute appengine_config.py so lib folder was not registered as a dependency folder. To check you can try printing something in appengine_config.py to check if it's being executed on server startup.
tldr: use appengine_config.py and copy your virtualenv to a folder called lib, then make SURE you are running the app via dev_appserver.py
(the below is via bash in ubuntu)
SO after a long battle, I find that virtual env and gcloud dont play nice -
I copied everything from my virtual env dir
.../.virtualenvs/nimble/local/lib/python2.7/site-packages
into
[projectdir]/lib
and my appengine_config.py finally worked locally like it does in the cloud, but I absolutely HAVE to run
dev_appserver.py [my proj dir here]
or the google.appengine module wont load. did not know I should be using dev server. I feel very dumb.
for reference, heres the appengine_config.py
"""`appengine_config` gets loaded when starting a new application instance."""
print 'running app config yaya!'
from google.appengine.ext import vendor
vendor.add('lib')
print 'I am the line after adding lib, it should have worked'
import os
print os.getcwd()
Do you have Extra Libraries component for Python installed? It can be installed with
gcloud components install app-engine-python-extras
After installing this extra library you should be able to use built-in flask library without a problem. For more information, refer to this page
Source

Categories

Resources