首页 > 解决方案 > 如何搜索包含模块的 pip 包

问题描述

如果您继承了一些具有include's 的 python 代码,但您不知道之前安装了哪些包以满足这些include's,那么如何搜索包含该模块的 pip 包?

在这种情况下,我试图找到要安装的 pip 包,以满足:

from django.utils.encoding import smart_str, smart_unicode

通过安装Django,我能够满足smart_str,但我仍然没有smart_unicode

pip search没有帮助,因为它只搜索名称与您的搜索匹配的包。它不会告诉您哪些包包含某些特定模块或其他命名对象,例如smart_unicode. 大约有 100 万个软件包,django其名称中有一些变化。

在找出答案一次后,我将把它写入一个requirements.txt文件,以防止将来使用这个特定的 python 文件出现这个问题。

如何搜索包含模块的 pip 包?

标签: pythonpip

解决方案


您很可能正在使用 Python 3 和 Django 2。很可能您继承的代码是为支持 Python 2 的 Django 版本(可能是某些版本 1.XX)编写的,并且他们未能提供正确的 django 版本要求。因此,您必须明确指定 django 1.xx 的特定版本,同时这样做pip installpip install django=1.11有所帮助。

django 1.11这是我方便的代码中的一个快速片段


if six.PY3:
    smart_str = smart_text
    force_str = force_text
else:
    smart_str = smart_bytes
    force_str = force_bytes
    # backwards compatibility for Python 2
    smart_unicode = smart_text
    force_unicode = force_text

smart_unicode因此,将代码中所有出现的 替换为smart_text将使其工作并使其稍微面向未来(至少那部分)。

从长远来看,考虑迁移到 Python 3 和受支持的 Django 版本。


推荐阅读