Search code examples
python-3.xgoogle-cloud-platformgoogle-cloud-functionsjwtpyjwt

Google Cloud Functions - ImportError: cannot import name 'InvalidKeyError' from 'jwt.exceptions'


I am getting the below error in GCP while executing Cloud Functions i.e. Cloud PubSub with Python 3.8, also below is the packages included in the requirements.txt

I have also tried only with jwt installed, but got the same error. tried only with pyjwt, again got the same error. Tried with both, again same error.

Requirements.txt:

  • atlassian-python-api==3.13.0
  • google==3.0.0
  • google-api-python-client==2.18.0
  • google-auth==2.0.1
  • google-auth-httplib2==0.1.0
  • google-auth-oauthlib==0.4.5
  • oauth2client==4.1.3
  • oauthlib==3.1.1
  • sendgrid==6.8.1
  • gspread==4.0.1
  • pandas==1.3.2
  • jwt==1.2.0
  • PyJWT==2.1.0
  • cryptography==3.4.8
  • rsa==4.7.2

Traceback:

Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 152, in view_func
    function(data, context)
  File "/workspace/main.py", line 72, in get_data_from_jira
    data = jira_instance.jql("project = PROJECTNAME AND issuetype = 'ISSUETYPE' AND status = 'In Progress'")
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/atlassian/jira.py", line 2418, in jql
    return self.get(url, params=params)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/atlassian/rest_client.py", line 264, in get
    response = self.request(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/atlassian/rest_client.py", line 218, in request
    response = self._session.request(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests/sessions.py", line 528, in request
    prep = self.prepare_request(req)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests/sessions.py", line 456, in prepare_request
    p.prepare(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests/models.py", line 320, in prepare
    self.prepare_auth(auth, url)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests/models.py", line 556, in prepare_auth
    r = auth(self)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests_oauthlib/oauth1_auth.py", line 108, in __call__
    r.url, headers, _ = self.client.sign(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 351, in sign
    ('oauth_signature', self.get_oauth_signature(request)))
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 186, in get_oauth_signature
    sig = self.SIGNATURE_METHODS[self.signature_method](base_string, self)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 742, in sign_rsa_sha1_with_client
    return _sign_rsa('SHA-1', sig_base_str, client.rsa_key)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 617, in _sign_rsa
    alg = _get_jwt_rsa_algorithm(hash_algorithm_name)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 562, in _get_jwt_rsa_algorithm
    import jwt.algorithms as jwt_algorithms
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/jwt/algorithms.py", line 5, in <module>
    from .exceptions import InvalidKeyError
ImportError: cannot import name 'InvalidKeyError' from 'jwt.exceptions' (/layers/google.python.pip/pip/lib/python3.8/site-packages/jwt/exceptions.py)

Solution

  • My solution was just to reinstall pyjwt.

    Some answers to this question say the problem is with JWT and PyJWT libraries, but those answers didn't help me. I also tried to update these packages, but it didn't fix the problem. This issue occurred suddenly, before everything worked well despite the fact that I had the jwt and pyjwt libraries both installed.