Search code examples
python-3.xgoogle-app-enginegoogle-cloud-platforminstantsearch

Failed to import algoliasearch to my lib folder


I want to import algolia python lib to my python 3 app engine project.

So I did

pip install algoliasearch -t lib

And in my lib folder I created __init__.py

Then In my project I import using

from lib.algoliasearch.search_client import SearchClient

So far so good. But in algolia library, for instance

lib\algoliasearch\search_client.py

There is also import statements as below:

from algoliasearch.helpers import endpoint, is_async_available

this works when I installed globally otherwise for installed in lib folder, it does not work when I deploy app engine.

As a solution I could update those files

from lib.algoliasearch.helpers import endpoint, is_async_available

which is a terrible solution.

On the other hand, if I apply:

import sys
sys.path.insert(0, './lib') 

this time, it is broken at several part i.e.

libcrypto_path = find_library(b'crypto' if sys.version_info < (3,) else 'crypto')
if not libcrypto_path:
    raise LibraryNotFoundError('The library libcrypto could not be found')

at

lib\asn1crypto\_perf\_big_num_ctypes.py

How can I properly import this algoliasearch lib?


Note: I could guess, Algolia team should update their packages by giving relative paths such as:

instead of

from algoliasearch.helpers import endpoint, is_async_available

this:

from helpers import endpoint, is_async_available

Solution

  • As I haven't worked clearly, I installed everything to my lib folder in my application. In fact, only algolia package is enough to install to lib else is in global as long as it does not in app engine environment.

    What I do is as solution

    import os, sys
    sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
    

    attention it is not insert to first place, not to do this:

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

    otherwise, it overrides global package i.e.

    Traceback (most recent call last):
      File "C:\my_project\main.py", line 11, in <module>
        from algoliasearch.responses import Response
      File "C:\my_project\lib\algoliasearch\responses.py", line 3, in <module>
        from typing import List, Union, Optional
      File "C:\my_project\lib\typing.py", line 1356, in <module>
        class Callable(extra=collections_abc.Callable, metaclass=CallableMeta):
      File "C:\my_project\lib\typing.py", line 1004, in __new__
        self._abc_registry = extra._abc_registry
    AttributeError: type object 'Callable' has no attribute '_abc_registry'
    

    but the issue reveals is, as I work python app engine project in visual studio 2017, weirdly visual studio complains in some middle of the importing packages.

    As workaround to this, install algolia globally as well.