ImportError: cannot import name argparser - python

I have installed Google APIs Client Library for Python via easy_install.py --upgrade google-api-python-client. When I run a script that contains from oauth2client.tools import argparser, run_flow returns:
Traceback (most recent call last):
File "C:\Users\name\Desktop\file.py", line 9, in <module>
from oauth2client.tools import argparser, run_flow
ImportError: cannot import name argparser
I am using python 2.7.6 64x in win 8. The strange thing is that doing the same procedure in another pc in win 7 it working perfect. What is going wrong?
Thanks.

The argparser object was added on 29 March 2013, after google-api-python-client version 1.1 was released.
It appears your easy_install.py --upgrade google-api-python-client call failed to upgrade your local installation, you are still trying to import from version 1.1. The output of import oauth2client.tools; print oauth2client.tools.__file__ shows this:
C:\Python27\lib\site-packages\google_api_python_client-1.1-py2.7.egg\oauth2clie‌​nt\tools.pyc
You can see the version number in the egg path.

Related

Why am I getting this ImportError when trying to import docx in the Terminal?

I'm learning Python at the moment, and after installing the docx module, I keep getting this error when trying to import it in the Terminal:
import docx
Traceback (most recent call last):
File "", line 1, in
File "/Users/trevorstathatos/Library/Python/3.10/lib/python/site-packages/docx/init.py", line 14, in
from docx.parts.document import DocumentPart
File "/Users/trevorstathatos/Library/Python/3.10/lib/python/site-packages/docx/parts/document.py", line 7, in
from docx.document import Document
File "/Users/trevorstathatos/Library/Python/3.10/lib/python/site-packages/docx/document.py", line 10, in
from docx.section import Section, Sections
File "/Users/trevorstathatos/Library/Python/3.10/lib/python/site-packages/docx/section.py", line 7, in
from collections import Sequence
ImportError: cannot import name 'Sequence' from 'collections' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/init.py)
When trying to import it to my Mu Editor, I get this error:
import docx
~/Library/Application Support/mu/mu_venv-38-20211212-105811/lib/python3.8/site-packages/docx.py in
28 TAGS = {}
29
---> 30 from exceptions import PendingDeprecationWarning
31 from warnings import warn
32
ModuleNotFoundError: No module named 'exceptions'
Can someone please help me figure out how to fix this problem? Thanks.
I have readed on internet and a solution may be:
$ pip install python-docx
#instead of pip install docx
(for python 3.x)
Please try this and tell me if works
Link: https://flutterq.com/solved-import-no-module-named-exceptions/
Are you using the most recent version of python-docx? (currently 0.8.11). I found that when I used 0.8.10 python couldn't find all the modules.
You can use where python command in cmd to find the path that python is installed in. then you can use this command -
[python path] -m pip install python-docx
$ pip install python-docx
this worked for me.

Unable to import database module from firebase_admin

I'm simply trying to add the Firebase Admin SDK to my Python script, but am unable to import the database module due to a TypeError in one of the library's python scripts.
I installed the library as instructed:
sudo pip install firebase-admin
I initialized the Firebase Admin SDK as instructed:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
But it breaks:
>>> import firebase_admin
>>> from firebase_admin import credentials
>>> from firebase_admin import db
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/anaconda/lib/python3.6/site-packages/firebase_admin/db.py", line 33, in <module>
from firebase_admin import _http_client
File "/anaconda/lib/python3.6/site-packages/firebase_admin/_http_client.py", line 30, in <module>
raise_on_status=False, backoff_factor=0.5)
TypeError: __init__() got an unexpected keyword argument 'status'
I peeked into the problematic script _http_client.py and saw that it imports requests, so I updated that with pip, to no avail.
No idea what could be the problem here. Any help would be much appreciated! Thank you!
Turns out, there was an old version of urllib3 lurking in my requests package. Removing the former from the latter did the trick. Thanks to shmee and Hiranya Jayathilaka for leading me to the solution!
Leaving an answer here to help people who are googling this find it easily.
Link to issue 262 on GitHub
You can check your version with:
import requests
from requests.packages import urllib3
print(urllib3.__version__)
>>>"1.16.1" # my output
You can check the location of the urllib3 you are using with:
import requests
from requests.packages import urllib3
print(urllib3.__file__)
>>>'...anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/__init__.py'
If you are using Anaconda, you can physically remove the package, or you can just run conda update urllib3 in a terminal. That worked for me.
this worked for me , i just searched for the location of urllib3 package and then i deleted it .
you can find the location of the package by taping the following commands in python interpreter
from requests.packages import urllib3
print (urllib3.__file__)

python freeopcua import error: cannot import name Client

folks, i got python 3.5.0 installed on a windows 7 machine and used pip3 to install dependencies cryptography, dateutil, lxml and pytz as mentioned on the freeopcua homepage.
thereafter I installed freeopcua using pip3 as well.
when trying to run one of the examples https://github.com/FreeOpcUa/python-opcua/blob/master/examples/client_to_kepware.py I got the error
1 import sys
2 sys.path.insert(0, "..")
3 import logging
4
5 from opcua import Client
6 from opcua import uaprotocol as ua
"Traceback (most recent call last):
File "xxx\Desktop\opcua.py", line 5, in
from opcua import Client
File "xxx\Desktop\opcua.py", line 5, in
from opcua import Client
ImportError: cannot import name 'Client'"
in my directory "xxx\Python35-32\Lib\site-packages" I do see opcua and freeopcua-0.09.3-py3.5.egg-info so it appears to be intalled correctly.
inside opcua package there is a __init__ importing
from opcua.client.client import Client from a folder client that exists on the same level as __init__. that folder has a module client.py and that module is holding class "Client". so to me everything appears fine but I am not very experienced here.
Not sure what is causing this? thanks for help!
above issue did take me quite some time and after being stuck for one evening I decided to ask. However, I eductated myself on packages that morning and appears like sys.path(0..) is the reason. Not 100% sure why it is used but it somehow changes directory. After puting file into my Python directory /Pyton35-32 it is working

"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 module httplib2 and python path

I am trying to use httplib2 in Python 2.7 on Windows 7 using the IDLE PythonWin 32 build 219.
I downloaded it and installed using python setup.py install method.
On Windows command line the following is successful:
python
import httplib2
httplib2
<module 'httplib2' from 'C:\Python27\ArcGISx6410.2\lib\site-packages\httplib2-0.9.2-py2.7.egg\httplib2\__init__.pyc'>
Here's the problem: in PythonWin importing httplib2 returns:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ImportError: No module named httplib2
I added the location of the module returned in command line (above) using sys.path.append in command line hoping that would resolve the issue.
PythonWin still cannot import the module, and sys.path in PythonWin does not return the appended path to httplib2. I appended the path the same way in PythonWin, but still could not import the module, and when I reopened PythonWin, the path did not cotain the appended module anymore.
Why or how could PythonWin be using a different path, and how can I get PythonWin to be able to import httplib2?
Have you tried using the command line tool pip ? You can use it like so:
pip install httplib2
That should put it on your path. If you don't have pip installed see this post. Also, worth mentioning, httplib2 is not as friendly as requests which I personally prefer.

Categories

Resources