No module named "requests" when trying to use google OAuth2 with Docker - python

I am trying to use google OAuth for my web app. To do so I installed the packages google-api-python-client and google-auth in my venv and during my Docker build(from a requirements.txt). Despite this when I run my app it can't find the requests module, complaining that:
flask.cli.NoAppException: While importing "debateit", an ImportError was raised:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/google/auth/transport/requests.py", line 23, in <module>
import requests
ImportError: No module named 'requests'
The import is as follows:
from google.auth.transport import requests
and is used like:
idinfo = id_token.verify_oauth2_token(token, requests.Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
Other imports such as the id_token.verify_oauth2_token work fine.
I checked my docker build and it says I have included google-auth correctly:
Installing collected packages: ... google-auth, httplib2, google-auth-httplib2, google-api-python-client
Successfully installed ... google-api-python-client-1.7.3 google-auth-1.5.0 google-auth-httplib2-0.0.3 httplib2-0.11.3 ...
I can clearly see the google.auth.transport.requests module when I look in the venv, it just doesn't work in the app itself.
What am I missing? What could cause this module to not be found?

So I found out what was wrong - within the google.auth.transport.requests module they try to import the library "requests". I did not have this library installed. I have done so and it works now.
The guide I was following: https://developers.google.com/identity/sign-in/web/backend-auth did not mention that you need to install this library. I misunderstood what the import for requests in the requests module was supposed to do.

As reported in the documentation, it should be more likely like:
import google.auth.transport.requests
import requests
request = google.auth.transport.requests.Request()
credentials.refresh(request)
But for your purpose I'll suggest:
from google.auth.transport.requests import Request
then change the following from:
idinfo = id_token.verify_oauth2_token(token, requests.Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
to:
idinfo = id_token.verify_oauth2_token(token, Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
It was an error because inside the path google.auth.transport.requests there is no function or class which is named requests.
My suggestion is based on the line
idinfo = id_token.verify_oauth2_token(token, requests.Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
which show us that you use a class named Requests() which is present into google.auth.transport.requests as you can see in the documentation.

I know this was answered but you can quickly install those libraries globally by the running the command :
pip install google-auth
Here is how to install pip or installing the libraries in local virtual environment if someone doesn't want to install them globally:
https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

pip install requests
It seems like google.auth.transport.requests uses requests.

Related

Firebase import module fails with ModuleNotFoundError: No module named 'Crypto'

I am trying to launch a script I wrote that is supposed to read data from a firebase db but it throws the following error:
Traceback (most recent call last):
File "myScript.py", line 8, in <module>
from firebase import Firebase
File "/Users/georgeoprea/Library/Python/3.8/lib/python/site-packages/firebase/__init__.py", line 20, in <module>
from Crypto.PublicKey import RSA
ModuleNotFoundError: No module named 'Crypto'
I have tried to install Crypto in the following ways:
pip3 install Crypto
pip3 install pycryptodome
When I run pip3 show Crypto I get the following output:
Name: crypto
Version: 1.4.1
Summary: Simple symmetric GPG file encryption and decryption
Home-page: https://github.com/chrissimpkins/crypto
Author: Christopher Simpkins
Author-email: git.simpkins#gmail.com
License: MIT license
Location: /Users/georgeoprea/Library/Python/3.8/lib/python/site-packages
Requires: Naked, shellescape
Required-by:
This is the list of imports I have in myScript.py:
from bs4 import BeautifulSoup
import time
import smtplib
from datetime import datetime
import json
import random
from email.message import EmailMessage
from firebase import Firebase
OS info: macOS 12.1 running on M1 Pro.
What could I do to have my script recognise the Crypto module?
This might be somewhat related to this question.
/e:
Ok, since you are using this firebase package, I can hopefully help you out.
First of all, it's the package's fault that it isn't running. While it depends on many external packages, it has none of them defined.
This is what I had to do in a clean virtual environment just to be able to do from firebase import Firebase:
pip install sseclient python_jwt gcloud pycryptodome requests-toolbelt
Here is the requirements.txt I ended up with in the clean environment. Notice, that this was only for importing a single class from the package. There still might be other dependencies hidden somewhere, waiting to throw an exception.
I encourage you to give feedback to the developer about this issue. Even better, fix this yourself and open a Pull Request. You might help others with the same issue.
I found a workaround for this. I simply used another module to read from the firebase db. Instead of using firebase I used firebase_admin as mentioned in the firebase documentation. firebase_admin doesn't use Crypto so there's no more problem from this point of view. However I had to change a little bit how I retrieve and write data.

Error with Python SDK for IBM Watson when importing into Pycharm

The error I receive is:
DeprecationWarning: watson-developer-cloud moved to ibm-watson. To get
updates, use the new package.
service = watson_developer_cloud.AssistantV1(
I have tried updating watson-developer-cloud using pip install however I still have the same error.
The code used is below. All done in Python. Just left out the API key from the original code.
Any help is appreciated.
service = watson_developer_cloud.AssistantV1(
iam_apikey= '',
version= '2021-01-20'
import os
from pathlib import Path
import slack
import ibm_watson
import ibm_cloud_sdk_core
import watson_developer_cloud
from ibm_watson import AssistantV1
from dotenv import load_dotenv
)
See here for the instructions on that Python package for IBM Watson services. It is like stated in the warning:
watson-developer-cloud is now named ibm-watson. What you have to do is
pip install ibm-watson
or
pip install --upgrade ibm-watson
Because the packagae is named ibm-watson, you would need to use that name for import...
import ibm-watson
or
from ibm_watson import AssistantV1
See the linked repository for examples.

Unable to import 'flask_uploads' pylint(import-error)

I installed flask-upload module in windows 10:
pip install flask flask-wtf flask-uploads
The results were:
Successfully installed Jinja2-2.11.2 MarkupSafe-1.1.1 WTForms-2.3.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.2 flask-uploads-0.2.1 flask-wtf-0.14.3 itsdangerous-1.1.0
Then in the text editor there is an error when I import the module as shown in the screenshot. unable to import flask-uploads
After running the app.py the following are the errors in cmd:
Error: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "c:\users\seanv\onedrive\documents\web dev\##pprojects\flask\flask_uploads\myenv\lib\site-packages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "C:\Users\seanv\OneDrive\Documents\web dev\##pprojects\flask\flask_uploads\app.py", line 4, in <module>
from flask_uploads import configure_uploads, IMAGES, UploadSet
File "c:\users\seanv\onedrive\documents\web dev\##pprojects\flask\flask_uploads\myenv\lib\site-packages\flask_uploads.py", line 26, in <module>
from werkzeug import secure_filename, FileStorage
ImportError: cannot import name 'secure_filename' from 'werkzeug' (c:\users\seanv\onedrive\documents\web dev\##pprojects\flask\flask_uploads\myenv\lib\site-packages\werkzeug\__init__.py)
May someone who understands the problem help me with possible solutions or suggestions. Thank you in advance.
Your app is using Flask-Uploads.
Back in February 2020, there was an update for Werkzeug, a library which Flask and many libraries, including Flask-Uploads, is based on.
This update introduced a breaking change, as Werkzeug changed its API, ie. the import of secure_filename.
I provided a pull request to Flask-Uploads, which the maintainer accepted. But very sadly and unfortunately the maintainer did not want to provide a new package for PyPi.
So, while you could install the updated Flask-Uploads via a commit id from its GitHub repository, you cannot any longer install it from PyPi.
I asked the maintainer for a new release, I also offered my help, but no chance.
So, finally, I decided to fork the library.
Here is the new package on PyPi
https://pypi.org/project/Flask-Reuploaded/
Here is the repository
https://github.com/jugmac00/flask-reuploaded
It is a drop-in replacement. So you just have to install the new package and it just works. No need to change any imports or code in your application.

"ImportError: file_cache is unavailable" when using Python client for Google service account file_cache

I'm using a service account for G Suite with full domain delegation. I have a script with readonly access to Google Calendar. The script works just fine, but throws an error (on a background thread?) when I "build" the service. Here's the code:
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
import urllib
import requests
from apiclient.discovery import build
cal_id = "my_calendar_id#group.calendar.google.com"
scopes = ['https://www.googleapis.com/auth/calendar.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('my_cal_key.json', scopes=scopes)
delegated_credentials = credentials.create_delegated('me#mydomain.com')
http_auth = delegated_credentials.authorize(Http())
# This is the line that throws the error
cal_service = build('calendar','v3',http=http_auth)
#Then everything continues to work normally
request = cal_service.events().list(calendarId=cal_id)
response = request.execute()
# etc...
The error thrown is:
WARNING:googleapiclient.discovery_cache:file_cache is unavailable when using oauth2client >= 4.0.0
Traceback (most recent call last):
File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
from google.appengine.api import memcache
ImportError: No module named 'google'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
from oauth2client.contrib.locked_file import LockedFile
ImportError: No module named 'oauth2client.contrib.locked_file'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module>
from oauth2client.locked_file import LockedFile
ImportError: No module named 'oauth2client.locked_file'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/__init__.py", line 41, in autodetect
from . import file_cache
File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/file_cache.py", line 41, in <module>
'file_cache is unavailable when using oauth2client >= 4.0.0')
ImportError: file_cache is unavailable when using oauth2client >= 4.0.0
What's going on here and is this something that I can fix? I've tried reinstalling and/or upgrading the google package.
I am a bit late to the party here but I had a similar problem today and found the answer here
Solution to only the error : file_cache is unavailable when using oauth2client >= 4.0.0
Solution:
change your discovery.build() to have the field cache_discovery=False
i.e
discovery.build(api, version, http=http, cache_discovery=False)
The code head of module "google-api-python-client" said...
install_requires = [
'httplib2>=0.9.2,<1dev',
'oauth2client>=1.5.0,<5.0.0dev', <<=============
'six>=1.6.1,<2dev',
'uritemplate>=3.0.0,<4dev',
]
So, I have uninstalled oauth2client version 4.0.0
Then, I have downloaded oauth2client 1.5.2 in a tar.gz file from offial python site https://pypi.python.org/pypi/oauth2client/1.5.2
I have installed this downloaded file so I have 1.5.2 version of oauth2client
Package Version
------------------------ ---------
certifi 2016.9.26
discovery 0.0.4
distribute 0.7.3
future 0.16.0
google-api-python-client 1.5.5
httplib2 0.9.2
oauth2client 1.5.2
pefile 2016.3.28
pip 9.0.1
pyasn1 0.1.9
pyasn1-modules 0.0.8
PyInstaller 3.2
pypiwin32 219
requests 2.11.1
rsa 3.4.2
setuptools 28.8.0
six 1.10.0
uritemplate 3.0.0
After that, ALL is working OK again and there is no warning message.
Tried all the solutions that were listed, but none of them worked. Until I tried the (simple) suggestion from #dtk that was listed somewhere in the comments :
Install an older version of oauth2client by running:
pip install oauth2client==3.0.0
Now everything works fine for me. Thank you #dtk !
I did not want to downgrade my oauth2client. You can use oauth2client 4.1.2 when you set cache_discovery=False for apiclient.discovery.build. This simple solution silenced the error:
service = discovery.build(api_name,
api_version,
credentials=credentials,
cache_discovery=False)
Source
To use the Google API for Python client, you need to install it first as Google API is not built-in within the Python modules. The instruction is found in Install the Library.
Installation
You can either use a package manager or download and install the Python client library manually:
Managed installation
Use pip or setuptools to manage your installation (you might need to run sudo first):
pip (preferred):
$ pip install --upgrade google-api-python-client
Setuptools: Use the easy_install tool included in the setuptools package:
$ easy_install --upgrade google-api-python-client
Fwiw this is a reposting of my answer here
Since they are technically warnings (not errors), if you don't have direct access to the call, you can do this wherever you instantiate the service to suppress them:
import logging
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)
With thanks to theacodes who gave that answer here (but with a missing 'n' in 'client' that seems to have led to a lot of downvotes there).
See also the discussion here
to add to the selected answer, if the build function is called by a dependency, and there is no easy way to update it's code, this code can be used to force cache_discovery to be False:
import googleapiclient.discovery
_build = googleapiclient.discovery.build
def no_cache_build(*args, **kw):
kw["cache_discovery"] = False
return _build(*args, **kw)
googleapiclient.discovery.build = no_cache_build
Here is how I use google-api-python-client to build a service on Cloud Functions.
# for Cloud Functions use
def get_service():
import googleapiclient.discovery
return googleapiclient.discovery.build('compute', 'v1', cache_discovery=False)
And finally it worked.

Python-Instagram API example not working

I am looking to use the Python-Instagram package. I have downloaded the package using the script that is provided on https://github.com/Instagram/python-instagram.
I have attempted one of the scripts that is provided. However, I get the same error: ImportError: No module named client
from instagram.client import InstagramAPI
access_token = "YOUR_ACCESS_TOKEN"
api = InstagramAPI(access_token=access_token)
recent_media, next_ = api.user_recent_media(user_id="userid", count=10)
for media in recent_media:
print media.caption.text
What is the module client and how can I install it? I have registered a client with IG.
Did you put the name "instagram" on the file? This might be your problem.
After seeing this link, which describes the same error, I realized my mistake.
I just had this same problem and it turned out that I had file named instagram.py in the same package as the file where I was getting this error. So, it was just a name collision. I renamed my local file and everything worked fine.
The recommended way:
Make sure you have pip installed
Install the package with the command: pip install python-instagram (you may require administrator/sudo privileges for this)
Manual Installation:
Download the zip file from https://github.com/Instagram/python-instagram
Copy the instagram directory to your python's dist-package directory or site-package directory (whatever is relevant to you)
You should then be able to import InstagramAPI with
from instagram.client import InstagramAPI
You may end up here like I did, and even by having
from InstagramAPI import InstagramAPI
and everything else correctly set I still got the error:
ImportError: No module named InstagramAPI
I was stupid enough to have InstagramAPI installed in one venv and was using another venv to run my project... dah

Categories

Resources