google-app-engine firebase-admin python - python

I'm attempting to setup firebase-admin with google-app-engine standard python. My dev environment is windows and I've followed the library setup as indicated in how to install 3rd party libraries. The firebase website indicates that firebase-admin has been tested on app engine, but there are no instructions or indication as to whether it was tested in standard, flexible or both. I've started with the most basic example and just tried the first import from the firebase generic documentation.
import webapp2
import firebase_admin
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
The result is
ImportError: The requests library is not installed, please install the
requests package to use the requests transport.
The requests library in lib during the install of firebase-admin so I'm not sure why I get this message. If I add import requests immediately before import firebase_admin I will instead get this message.
ImportError: No module named _winreg
I'd like to use firebase-admin if at all possible so if anyone is familiar with this situation and how to resolve it please let me know. Also, I am not interested in using the flexible environment, this is a question for the standard environment only.

-Install necessary modules under lib;
pip install -t lib/ firebase-admin
pip install -t lib/ requests-toolbelt
-you can delete the .pyc files as they are just the precompiled versions of the .py files that are also there (and will be regenerated when .py is executed).
-add below code in added the appengine_config.py;
from google.appengine.ext import vendor
vendor.add('lib')
import requests
import requests_toolbelt.adapters.appengine
requests_toolbelt.adapters.appengine.monkeypatch()
import platform
def patch(module):
def decorate(func):
setattr(module, func.func_name, func)
return func
return decorate
#patch(platform)
def platform():
return 'AppEngine'
see https://groups.google.com/forum/#!topic/firebase-talk/FzEG2U6SRN8

Related

No module named 'openpyxl' when trying to import it from flask app

No module named 'openpyxl' when trying to import it from flask app , pointing that the module is installed in "/home/ubuntu/.local/lib/python3.8/site-packages/" and it works fine in other scripts
here what i am using
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/upload_file', methods=['GET','POST'])
def upload_file():
try :
import sys
sys.path.append('/home/ubuntu/.local/lib/python3.8/site-packages/')
import openpyxl
except Exception as e:
return render_template('index.html',msg=str(e))
return render_template('index.html',msg=request.method)
i've seen a lot of questions about how to import a module in flask since i've found it as a common error , so i've tried to create a module that does nothing but import openpyxl but no luck either i had the same error
I am using ubuntu 20.04 python3.8
Thanks in advance
I found two solutions :
1 .
Install the module manually in /usr/local/lib/python3.8/dist-packages using python3 -m pip3 install openpyxl in that directory
2 .
change the directory where the modules are installed using pip by creating the config file ~/.config/pip/pip.conf , the content must be your desired install location like this :
[global]
target = /usr/local/lib/python3.8/dist-packages
I hope this help someone with the same need

GAE: ImportError while using google-auth

I am using google-auth to allow firebase authentication in my GAE project.
Everything works fine when I run the code locally using dev_appserver.py or when I deploy it to google app engine.
But I get this ImportError exceptions when I try to use Django's manage.py script to create/run migrations.
ImportError: Could not import 'firebase.authentication.FirebaseAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: No module named auth.transport.requests.
The google-auth module is installed under lib directory and has this structure:
- lib
- google
- auth
- oauth2
These import cause the ImportErrors:
import google.auth.transport.requests
from google.oauth2 import id_token
My guess is that there might be naming conflicts as other imports work fine.
Please help!
If you want to use 3rd party libraries that are not included in this list, then you'll have to add them manually since you have done that by adding lib folder and including all the package folder follow these steps.
Create your_app_directory/appengine_config.py file.
Add these following lines inside that file
from google.appengine.ext import vendor
vendor.add('lib')
This should solve the import error problem.

Google App Engine Locally: ImportError: No module named google.cloud.bigquery

As the title says.
So I've added the following to appengine_config.py with no luck:
# appengine_config.py
from google.appengine.ext import vendor
# Add any libraries install in the "lib" folder.
vendor.add('lib')
I did a print sys.path and verified the lib dir contains google/cloud/bigquery
I can import it if I run python myself:
from google.cloud import bigquery
print bigquery.__path__
['/usr/local/lib/python2.7/dist-packages/google/cloud/bigquery']
From a Google App Engine end points:
import google.cloud;print google.cloud.__path__
['/usr/local/lib/python2.7/dist-packages/google/cloud']
Big Query is at that system location. /usr/local/lib/python2.7/dist-packages/google/cloud/biqguery/ exists. However if I try the following from app engine end point:
from google.cloud import bigquery
ImportError: No module named google.cloud.bigquery
The file in question includes and that does not appear to help:
from __future__ import absolute_import
Update
I setup a venv and install everything like this: pip install -t lib/ -r requirements.txt --upgrade. From there if I try import google; print google.__path__ I get:
['lib/google', '/usr/lib/google-cloud-sdk/platform/google_appengine/google']
I think the second path might be the cause.
I've reviewed the following with no success:
Error importing Google Cloud Bigquery api module in python app
https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2366
How to import BigQuery in AppEngine for Python
You need to install google-cloud-bigquery into you app's lib dir, that's where the development server is looking at, not on your system's libs. From Installing a third-party library:
Create a directory to store your third-party libraries, such as lib/.
mkdir lib
Use pip (version 6 or later) with the -t <directory> flag to copy the libraries into the folder you created in the previous
step. For example:
pip install -t lib/ <library_name>

Python - Can't import bcrypt in local app engine dev server

I am trying to develop a simple forum site for my udacity assignment. It is not a strict requirement to use the bcrypt for password hashing, but I'd like to do it because I also like to know how to use third party libraries which are not provided by Google.
Following instructions provided here (installing a third-party library), I have created a folder named lib, and installed bcrypt library with following command:
python -m pip install -t lib/ bcrypt
I have the lib folder automatically structred like this:
I also created an appengine_config.py file with following content, as per instructions in above manual:
# appengine_config.py
from google.appengine.ext import vendor
# add lib folder as vendor directory
vendor.add('lib')
At this point, I am unable to import the bcrypt to my scripts. The import commands I tried so far are as follows:
from lib import bcrypt
ImportError: No module named lib
import bcrypt
ImportError: No module named bcrypt._bcrypt
from lib.bcrypt import bcrypt
ImportError: No module named lib.bcrypt
What am I missing?
As Avinash Raj pointed out, and as already pointed out in referenced manual, one cannot use python libraries with c extensions. So I downloaded the py-bcrypt, it worked like a charm.
For any newbie like me who needs it, here is the steps you have to take:
Inside your project folder, create a folder called "lib"
Extract the zip downloaded from github above, to the folder 'lib'. Do not use
- in your folder name. Name it something like pybcrypt
Create the appengine_config.pyfile, as outlined in here
Import the library to your script, like so: from pybcrypt import bcrypt
Pat yourself on the back.
Here is another option, you need to setup Wheel package before you can import bcrypt
pip install wheel
pip install bcrypt
from flask_bcrypt import Bcrypt

GAE import errors; sys.path shows wrong path for appengine libraries

I'm developing a web app for Google App Engine in Python on Windows 10. Everything was working fine when my main.py was just serving templates.
import os
import urllib
from google.appengine.api import users
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
But then I tried to add cloud storage, and got import errors no matter what library I tried. So I removed those references from main.py, and now I get an error with jinja2, which had been working fine!
ImportError: No module named jinja2
I don't remember everything I tried, but here's what I do know:
jinja2 is installed; attempts to pip install/upgrade says it's already installed and up-to-date, and I see it in c:\python27\lib\site-packages.
PYTHONPATH=C:\python27;c:\python27\lib;C:\Python27\DLLS for system and user.
At one point I had installed GoogleAppEngineCloudStorageClient with PIP into my app's lib directory per this. It didn't work (the module failed to import), so I also tried adding sys.path.append(os.path.join(os.path.dirname(__file__), "lib")) but it didn't help. I think this is when I started getting the jinja2 import error. So I removed the statement from main.py. Still getting the jinja2 import error. I tried pip uninstall GoogleAppEngineCloudStorageClient but it said it wasn't installed, so I tried just deleting the lib directory. Still getting the jinja2 import error.
Have tried restarting the service and rebooting my machine.
EDIT:
I stripped main.py all the way to the new project template,
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world!')
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
and now I get an import error for webapp2:
ImportError: No module named webapp2
EDIT 2:
By inserting this at the top of my main.py,
import sys
print sys.path
I can see the wrong path for all the google packages:
'C:\\Program Files (x86)\\Google\\lib\\webapp2-2.5.2',
'C:\\Program Files (x86)\\Google\\lib\\pycrypto-2.6',
'C:\\Program Files (x86)\\Google\\lib\\jinja2-2.6',
'C:\\Program Files (x86)\\Google\\lib\\markupsafe-0.15',
'C:\\Program Files (x86)\\Google\\lib\\setuptools-0.6c11',
'C:\\Program Files (x86)\\Google\\lib\\protorpc-1.0',
'C:\\Program Files (x86)\\Google\\lib\\webob-1.1.1',
'C:\\Program Files (x86)\\Google\\lib\\yaml-3.10'
They are actually in C:\Program Files (x86)\Google\google_appengine\lib
I don't know why I didn't have this problem before I tried to install that one package, but this may be related to a reported google issue.
Update: the issue was fixed in SDK version 1.9.40.
There is a GAE issue causing exactly this behaviour introduced in SDK version 1.9.37, see "ImportError: No module named webapp2" after Linux SDK upgrade (1.9.35 -> 1.9.38).
If your SDK version is 1.9.37 or 1.9.38 downgrade to 1.9.36, which you can find here. At least until the fix gets released.
Summary:
The webapp2 and jinja2 import errors are caused by sys.path corruption, a result of a GAE defect present in versions 1.9.37 or 1.9.38. It only impacts development; deployed versions should work. It can occur immediately upon upgrading or after attempting to install other items.
Solutions:
Downgrading the AppEngineSDK to 1.9.36 worked for me.
The GAE defect will be fixed at some point after 1.9.38. Check for Issue 12963 for status.
Per Google issue 13084, another workaround is to manually patch sys.path in appengine_config.py. Docs here.

Categories