# Google API Python Client Import Issue

**URL:** <https://forums.ankiweb.net/t/google-api-python-client-import-issue/30091>\
**Category:** Development\
**Created:** [May 7, 2023, 9:22am UTC](https://forums.ankiweb.net/t/google-api-python-client-import-issue/30091 "2023-05-07T09:22:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CravingCrates](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/cravingcrates/32/16194_2.png) [@CravingCrates](https://forums.ankiweb.net/u/CravingCrates)\
**Post date:** [May 7, 2023, 9:22am UTC](https://forums.ankiweb.net/t/google-api-python-client-import-issue/30091/1 "2023-05-07T09:22:42Z")

</div>

Hey,  
I’m having some issues getting the google-api-python-client to work on Anki.

I installed the necessary packages with pip to a target folder inside the Anki Add-Ons Folder and added the dist Folder to the sys path like I do with any other third party package.

In the following example:  
`pip install -t . functional google-auth google-auth-httplib2 google-api-python-client --upgrade`

It works fine for other packages I tested, but appears to break on googles even though it gets properly installed.

A minimal example would be this:

AddOnFolder/  
AddOnFolder/dist/  
AddOnFolder/main.py  
AddOnFolder/ **init**.py

main.py looks like this:

```auto
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname( __file__ ), "dist"))

import functional
import google.oauth2

```

I only added the functional to show that other packages work fine from the same folder.

Now what puzzles me is that when I run the same main.py as a standalone and not via anki, it works fine so I don’t believe it’s a missing dependency or path issue. Only when I let Anki run the main it fails with the error:  
(Note that it accepts the functional import in the line above just fine).

```auto
main.py", line 7, in <module>
    import google.oauth2
ModuleNotFoundError: No module named 'google.oauth2'
<U+2069>

```

I believe it is caused by the fact that the Anki Directory Anki/lib/ contains a folder called google and thus interferes with the import of the google package in dist/google/ since it’s used as a search path.

I thought adding the dist/ path at the front of the sys.path would solve it (since python checks in the directories in sequential order), but it doesn’t.

Renaming the dist/google/ to something else and changing the `import google.oauth2` to the new name works, but causes more issues down the line since the google api itself uses imports from google.x , too.

I’m honestly not sure how to approach this issue or fix it and would greatly appreciate any help or guidance.

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [May 7, 2023, 10:50am UTC](https://forums.ankiweb.net/t/google-api-python-client-import-issue/30091/2 "2023-05-07T10:50:08Z")

</div>

I once had a similar problem when trying to use the google-cloud-texttospeech module in an add-on. I don’t remember the exact details of the problem, but something like this worked for me:

```python
sys.path.insert(0, os.path.join(addon_path, "vendor"))
google_path = os.path.join(addon_path, "vendor", "google")
source = os.path.join(google_path, " __init__.py")
spec = importlib.util.spec_from_file_location(
    "google", source, submodule_search_locations=[]
)
module = importlib.util.module_from_spec(spec)
sys.modules["google"] = module
spec.loader.exec_module(module)
del sys.modules["google.protobuf"]

from google.cloud import texttospeech

```

(I might be wrong and your problem could be different though)

---

<div class="post-metadata">

**Author:** ![CravingCrates](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/cravingcrates/32/16194_2.png) [@CravingCrates](https://forums.ankiweb.net/u/CravingCrates)\
**Post date:** [May 7, 2023, 7:38pm UTC](https://forums.ankiweb.net/t/google-api-python-client-import-issue/30091/3 "2023-05-07T19:38:53Z")

</div>

This is amazing. Not sure why, but your snippet did the trick for me too.

Thanks a lot for your help. You saved me hours of debugging \<3

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [June 6, 2023, 7:39pm UTC](https://forums.ankiweb.net/t/google-api-python-client-import-issue/30091/4 "2023-06-06T19:39:11Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
