0

Google OAuth2 implementation in Python is packaged in such a way that I have to

from google.oauth2 import service_account

credentials = service_account.Credentials...

Since I prefer to be explicit with the place my external methods come from, I would prefer very much to

import google.oauth2

credentials = google.oauth2.service_account.Credentials...

but this does not work, an answer to another question neatly explains why (there are also many other good explantions, more or less ion-depth).

Is there a way to mimic the "full path notation" so that I can use the second version, with the restrictions forcing me to use from module import ...?

I tried

from google.oauth2 import service_account as google.oauth2.service_account

credentials = google.oauth2.service_account.Credentials....

but I get

  File "d:\Nextcloud\dev-perso\testing\googlecalendar\googlecalendar.py", line 4
    from google.oauth2 import service_account as google.oauth2.service_account
                                                       ^
SyntaxError: invalid syntax

Is there another solution, or am I stuck with the short names?

Note: I could

from google.oauth2 import service_account as google_oauth2_service_account

credentials = google_oauth2_service_account.Credentials...

but this is not that elegant (which is a subjective opinion, and which I will end up using if there are no better solutions)

WoJ
  • 27,165
  • 48
  • 180
  • 345
  • You must be a Java programmer ;-) Use the short names, and make your modules short, that way there is no confusion as to where names are coming from - in addition you don't need to type so much, other programmers don't need to read so much, and you're following the standard Python way of doing things (it's a win-win-win!) – thebjorn Jan 31 '20 at 07:37
  • You do realise that if you want to use ˋgoogle.oauth2.service_accountˋ, you must ˋimport google.oauth2.service_accountˋ, not just ˋimport google.oauth2ˋ? – MisterMiyagi Jan 31 '20 at 07:50
  • @thebjorn ˋgoogleˋ is an external package, it’s not up to the OP to change its layout. – MisterMiyagi Jan 31 '20 at 07:52
  • @MisterMiyagi I'm saying to just use `from google.oauth2 import service_account` as every other Python programmer expects you to do (including the people who wrote the docs for this package). – thebjorn Jan 31 '20 at 07:57
  • @thebjorn: no I am just an amateur/home Python developer for 10 years or so. I find that using `frac` instead of `math.extended.rationsl.frac` makes reading code way more difficult. But as I mentioned this is subjective and not related to the question. I am glad you know all Python programmer in the world, since "every other one" would use the short form. – WoJ Jan 31 '20 at 08:12
  • You should stop at the module level (not the function level), ie. `from math.extended import rationsl` then use `rationsl.frac(...)`. This should give enough context in any well-designed package. – thebjorn Jan 31 '20 at 08:19
  • ps: of course I know every other Python programmer - at least for a suitable definition of "every" :-) – thebjorn Jan 31 '20 at 08:21

1 Answers1

4

You could just import the module service_account by using absolute path:

import google.oauth2.service_account
google.oauth2.service_account.Credentials
#<class 'google.oauth2.service_account.Credentials'>
ExplodingGayFish
  • 2,807
  • 1
  • 5
  • 14