ImportError: cannot import name 'ServiceAccountCredentials' from 'oauth2client.service_account' - python

I'm trying to follow along a tutorial on pulling in Google Analytics data using Python.
Having followed the steps throughout the tutorial, when I call the script I get the error message in the title:
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredential
Traceback (most recent call last):
File "ga.py", line 4, in
from oauth2client.service_account import ServiceAccountCredentials
ImportError: cannot import name 'ServiceAccountCredentials' from 'oauth2client.service_account'
I was reading this SO post after some Googling.
I tried changing my package version:
pip install oauth2client==1.5.2
I tried adding new packages per the answers on that post:
pip install pyopenssl
pip install pycrypto
pip install httplib2
pip install oauth2client
pip install ssl
In each case I encountered the same message after trying to run ga.py:
(zen_ga) Macs-MacBook:zen_ga macuser$ python ga.py
Traceback (most recent call last):
File "ga.py", line 4, in
from oauth2client.service_account import ServiceAccountCredentials
ImportError: cannot import name 'ServiceAccountCredentials' from 'oauth2client.service_account'
How can I import ServiceAccountCredentials from oauth2client.service_account?

First, the class you are trying to import is called ServiceAccountCredentials, not ServiceAccountCredential. So try this instead: from oauth2client.service_account import ServiceAccountCredentials.
oauth2client 1.5.2 does not include ServiceAccountCredentials. Use a more recent version to use it - the latest version is 4.1.3.
Finally, you should note that oauth2client is deprecated, and it is now recommended to use google-auth instead.

My 2cents:
For this to work I had to install and update these packages either
-from WITHIN the NORMAL TERMINAL with PLAIN PYTHON, no distribution such as Conda(dont forget to set Path-Variable):
py -m pip install google-api-python-client
py -m pip install oauth2client
-or from WITHIN the CONDA TERMINAL with:
pip install google-api-python-client oauth2client
pip install --upgrade oauth2client #important
this did NOT WORK when I tried to install and update from jupyter notebook with pip

It works if you import the module alone and then call the class in your code.
Change the import line to:
from oauth2client import service_account
and call the class with the module prefixed like so:
service_account.ServiceAccountCredentials()

Related

Error using azure-eventhubs python example

I'm attempting to use the example code to receive data from Azure Event Hub.
Heres the code
import os
import sys
import logging
import time
from azure.eventhub import EventHubClient, Receiver, Offset
logger = logging.getLogger("azure")
ADDRESS = "amqps://mine.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=sadfsafdljksad=<eventhub>"
But I am getting the following error when executing..
Traceback (most recent call last):
File "./recv.py", line 5, in <module>
from azure.eventhub import EventHubClient, Receiver, Offset
ImportError: cannot import name 'EventHubClient'
Any assistance will be appreciated.
I think you actually installed azure-eventhub package, version 5.0.0. But EventHubClient does not exist in 5.0.0 version, it exists in azure-eventhub package, version 1.3.3 or below.
By default, when you install the azure eventhub package for python using this command pip install azure-eventhub, it will automatically install the latest version 5.0.0. So this sentence from azure.eventhub import EventHubClient, Receiver, Offset will throw the error ImportError: cannot import name 'EventHubClient'
You can check the version of the package by open cmd -> input command pip show azure-eventhub. Screenshot as below:
There are 2 solutions:
Solution 1: keep using the 5.0.0 version, then you should re-write your code and follow the examples in this doc.
Solution 2: If you want to use the current code which includes from azure.eventhub import EventHubClient, Receiver, Offset, you should first uninstall the 5.0.0 version of azure-eventhub, then reinstall the 1.3.3 version by using this command pip install azure-eventhub==1.3.3
Using this solution works fine
reinstall the 1.3.3 version by using this command pip install azure-eventhub==1.3.3

Python can't find installed module slackclient on MacOs. Any suggestions?

I'm developing a slackbot. After importing slackclient, I got ModuleNotFoundError: No module named 'slackclient'.
I tried all the options and followed suggestions showed in the post here- Python can't find installed module ('slackclient'). By those suggestions, I installed slack but got the following error while importing WebClient.
>>> from slack import WebClient
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'WebClient' from 'slack' (<path_to _venv>/.venv/lib/python3.7/site-packages/slack/__init__.py)
I checked the slack version that seems like ok
slack 0.0.2
Any suggestions what could I be doing wrong?
Using slackclient version 2
$ pip install slackclient --upgrade
$ pip freeze
slackclient==2.1.0
from slack import WebClient
OR, Using slackclient version 1
$ pip install slackclient==1.3.1
from slackclient import SlackClient
there seems to be a conflict between slack versions and imports. If you are using slack >= 0.0.2 and slackclient >= 0.36.2, try this instead:
from slack.web.client import WebClient
you can check that WebClient class is defined in that directory.
I was able to get it to work by using
from slack.web.client import WebClient
The current version of slackclient for Python3 is 2.1.0.
To upgrade your environment run:
$ pip3 install slackclient --upgrade
You find the latest slackclient here.

No module named 'requests' when pip installing ssh-import-id

I'm using Heroku as a development server. When I try to push my Django application to Heroku it first tries to install my packages from the requirements.txt file.
requests==2.18.3
ssh-import-id==5.5
The problem is I have a dependency on one of my packages with others. In the above packages, ssh-import-id needs requests package already installed. So when I push the app, pip fails to install and stops the deployment.
Collecting requests==2.18.3 (from -r re.txt (line 1))
Using cached https://files.pythonhosted.org/packages/ba/92/c35ed010e8f96781f08dfa6d9a6a19445a175a9304aceedece77cd48b68f/requests-2.18.3-py2.py3-none-any.whl
Collecting ssh-import-id==5.5 (from -r re.txt (line 2))
Using cached https://files.pythonhosted.org/packages/66/cc/0a8662a2d2a781db546944f3820b9a3a1a664a47c000577b7fb4db2dfbf8/ssh-import-id-5.5.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-go0a5mxf/ssh-import-id/setup.py", line 20, in <module>
from ssh_import_id import __version__
File "/tmp/pip-install-go0a5mxf/ssh-import-id/ssh_import_id/__init__.py", line 25, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-go0a5mxf/ssh-import-id/
I need to install all the listed packages using pip in single attempt. Because by default Heroku runs, pip install -r requirements.txt.
This is a bug.
The library's setup.py imports the library to get the version for inclusion in the setup() function call...
import os
from setuptools import setup
from ssh_import_id import __version__
... and the library tries to import requests which doesn't yet exist in the environment. This is ssh_import_id.__init__.py:
import argparse
import json
import logging
import os
import platform
import requests # <=== here
import stat
import subprocess
import sys
import tempfile
A fix has been added which works around needing to import the package to get the version...
import os
from setuptools import setup
import sys
def read_version():
# shove 'version' into the path so we can import it without going through
# ssh_import_id which has deps that wont be available at setup.py time.
# specifically, from 'ssh_import_id import version'
# will fail due to requests not available.
verdir = os.path.abspath(
os.path.join(os.path.dirname(__file__), "ssh_import_id"))
sys.path.insert(0, verdir)
import version
return version.VERSION
... but the fix isn't in the current pypi version 5.6.
You could install latest master branch from source instead of pypi by changing your requirements.txt to something like:
-e git+https://git.launchpad.net/ssh-import-id#egg=master

Python import google.oauth2.credentials ImportError: No module named google.oauth2.credentials

i'm trying to run this bot https://github.com/ItsCEED/Youtube-Comment-Bot
in mac os but i get this error
l:Youtube-Comment-Bot-master ROOTXX$ python yt.py
Traceback (most recent call last):
File "yt.py", line 6, in <module>
import google.oauth2.credentials
ImportError: No module named google.oauth2.credentials
l:Youtube-Comment-Bot-master ROOTXX$
Run in a root terminal:
pip install --upgrade google-api-python-client
From here, found with a google search.
EDIT: Even better, you linked the required libraries yourself. They are in the GitHub README.md at here. Please read your own links. Anyway, the commands to install follow:
# pip install --upgrade google-api-python-client
# pip install --upgrade requests
# pip install --upgrade beautifulsoup4

How to install google.cloud with Python pip?

I am relatively new to Python and I am stuck on something which is probably relatively easy to resolve.
I have installed the following packages:
pip install --upgrade google-api-python-client
pip install --upgrade google-cloud
pip install --upgrade google-cloud-vision
In my Python file I have:
import cv2
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
...etc...
And this gives me the error:
Traceback (most recent call last):
File "test.py", line 6, in <module>
from google.cloud import vision
ImportError: No module named 'google.cloud'
What am I missing and where should I look (logs?) to find the answer in the future.
PS:
Pip installs of google-cloud and google-cloud-vision have the output:
Cannot remove entries from nonexistent file /Users/foobar/anaconda/lib/python3.5/site-packages/easy-install.pth
UPDATE:
Running pip freeze doesn't show the packages to be installed...
I had a similar problem. Adding "--ignore-installed" to my pip command made it work for me.
This might be a bug in pip - see this page for more details: https://github.com/pypa/pip/issues/2751
You need to download and install the google-cloud-sdk. Follow this link https://cloud.google.com/sdk/docs/
try this :
from google.cloud.vision import *

Categories