【问题标题】:Failed to import algoliasearch to my lib folder无法将 algoliasearch 导入我的 lib 文件夹
【发布时间】:2019-05-06 15:03:04
【问题描述】:

我想将 algolia python lib 导入我的 python 3 应用引擎项目。

原来如此

pip install algoliasearch -t lib

在我的 lib 文件夹中,我创建了 __init__.py

然后在我的项目中我使用导入

from lib.algoliasearch.search_client import SearchClient

到目前为止一切顺利。但例如在 algolia 库中

lib\algoliasearch\search_client.py

还有如下导入语句:

from algoliasearch.helpers import endpoint, is_async_available

这在我全局安装时有效,否则安装在 lib 文件夹中,当我部署应用引擎时它不起作用。

作为解决方案,我可以更新这些文件

from lib.algoliasearch.helpers import endpoint, is_async_available

这是一个糟糕的解决方案。

另一方面,如果我申请:

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

这一次,它在几个部分被打破,即

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')

lib\asn1crypto\_perf\_big_num_ctypes.py

如何正确导入这个 algoliasearch 库?


注意:我猜,Algolia 团队应该通过提供相关路径来更新他们的包,例如:

而不是

from algoliasearch.helpers import endpoint, is_async_available

这个:

from helpers import endpoint, is_async_available

【问题讨论】:

  • 您是否按照此处的说明创建了 app_config.py 文件? cloud.google.com/appengine/docs/standard/python/tools/…
  • 这适用于 python 2.7。我正在使用 python 3.7
  • 你能显示实际的导入错误(最好是完整的回溯)吗?
  • @DanCornilescu 没有堆栈跟踪但错误显示- __exception_info {'exception': ModuleNotFoundError(...asearch'"), 'exception_type': &lt;class 'ModuleNotFoundError'&gt;, 'message': "No module named 'al...iasearch'"} dict

标签: python-3.x google-app-engine google-cloud-platform instantsearch


【解决方案1】:

有什么理由不能将 algoliasearch 包作为依赖项包含在内,而不是像那样出售它?

例如,requirements.txt:

algoliasearch==2.0.0

(或您需要的任何版本)

【讨论】:

  • 您的建议确实有效 +1,看看我的回答为什么会变得如此复杂
【解决方案2】:

由于我没有清楚地工作,我将所有内容都安装到了我的应用程序的 lib 文件夹中。 实际上,只有 algolia 包就足以安装到 lib 中,否则只要不在应用引擎环境中,它就在全局中。

我做的是解决方案

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

注意它不是插入到第一位,不要这样做:

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

否则,它会覆盖全局包,即

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'

但问题是,当我在 Visual Studio 2017 中工作 python 应用程序引擎项目时, 奇怪的是,视觉工作室在导入包的某些中间抱怨。

作为解决此问题的方法,请在全局范围内安装 algolia。

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多