区分 Python 个同名模块/不同名称安装?

Distinguishing between Python modules of the same name / installing with different name?

为 Firebase REST 制作了两个单独的 Python 包装器 API:

https://github.com/mikexstudios/python-firebase

https://pypi.python.org/pypi/python-firebase/1.2

两者各有优缺点,所以现在我想用一个做一些API动作,另一个 对于其他 API 操作,在同一程序中。问题是,安装后,它们都被称为 firebase

是否可以 pip 使用不同的名称安装一个或两个?如果不是,如果使用正确,import语句是否具有区分的智能?

pip 无法做到这一点。 PyPI 上的所有包都有唯一的名称。包经常相互依赖和相互依赖,并且假定名称不会改变。

最好的办法是将库中的所有代码复制到代码库中,然后导入它。

导入模块时,python按顺序搜索sys.path中的路径,第一个匹配时停止。所以一个简单的 import firebase 是行不通的。

有一个选择其中之一的脆弱解决方案,但您将无法同时导入两者。

无论如何,要选择一个或另一个,您只需导入包的内部名称即可。如果我们查看这两个包的公开名称,我们会得到:

https://github.com/mikexstudios/python-firebase
 firebase/
  __init__.py
   Firebase
   requests
   urlparse
   os
   json

https://github.com/ozgur/python-firebase
 firebase/
  __init__.py
   atexit
   process_pool
   close_process_pool
   urlparse
   json
   FirebaseTokenGenerator
   http_connection
   process_pool
   JSONEncoder
   ...

因此,您可以通过导入仅存在于其中的名称来选择第一个:

from firebase import requests

或者第二种,同理:

from firebase import atext

但坦率地说,这在我看来很糟糕。