def count_installs(pkg_name):
import imp, sys
n = 0
for location in sys.path:
try:
imp.find_module(pkg_name, [location])
except ImportError: pass
else: n += 1
return n
def test_pip_python_packages_installed_twice(self):
# https://stackoverflow.com/a/23941861/633961
pkg_name_to_locations=defaultdict(set)
for dist in pkg_resources.working_set:
for pkg_name in dist._get_metadata('top_level.txt'):
for location in sys.path:
try:
importutils.does_module_exist_at_given_path(pkg_name, [location])
except ImportError:
continue
if location.startswith('/usr'):
# ignore packages from "root" level.
continue
pkg_name_to_locations[pkg_name].add(location)
errors=dict()
for pkg_name, locations in sorted(pkg_name_to_locations.items()):
if pkg_name in ['_markerlib', 'pkg_resources', 'site', 'easy_install', 'setuptools', 'pip']:
continue
if len(locations)==1:
continue
errors[pkg_name]=locations
self.assertFalse(errors,
'Some modules are installed twice:\n%s' % '\n'.join(['%s: %s' % (key, value) for key, value in sorted(errors.items())]))
这应该是有效的:
例如
^{pr2}$South-0.8.1-py2.7.egg
是一个zip归档文件,其源代码为South,South-0.8.4-py2.7.egg-info
是一个包含South库的元数据文件的目录。在.egg-info
(对于从.tar.gz
构建的库)或{.whl
安装的lib)都存在于pip
安装的每个库中。在如果库在元数据中标记为
zip_safe
(在setup.py
中setup(zip_safe=True)
),则创建.egg
存档。 否则,pip用提取的python源文件创建一个目录。在非常旧的setuptools版本能够安装同一个库的多个版本,并将其中一个标记为active,但如果我没记错的话,提到的功能多年前就被删除了。在
我使用此方法检查包是否安装了两次:
重要
^{pr2}$相关:imp.find_module() which supports zipped eggs
相关问题 更多 >
编程相关推荐