python - 如何搜索包含模块的 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 包?
解决方案
您很可能正在使用 Python 3 和 Django 2。很可能您继承的代码是为支持 Python 2 的 Django 版本(可能是某些版本 1.XX)编写的,并且他们未能提供正确的 django 版本要求。因此,您必须明确指定 django 1.xx 的特定版本,同时这样做pip install
会pip 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 版本。
推荐阅读
- c# - SQLiteConnection - “System.OverflowException”,但工作
- cookies - 有没有办法为 GKE 负载均衡器获取域而不是 ip?
- php - preg_match_all,查找所有包含特殊字符的精确匹配实例,例如 *** 并添加类
- thunderbird - gFolderDisplay 和其他未在 Thunderbird 后台脚本中定义的内置全局变量
- c - const double *bar = (const double *) foo;?
- r - 如何将多个整数绑定到一个向量中
- excel - 在 Excel 中将宏作为 VBA 事件运行
- python - 如何在 Windows 10 上安装 psycopg2?
- c - 如何在结构中分配内存使其连续
- html - 同时在 HTML 和 CSS 规则中的 style 标签