我用這個方法來檢查是否安裝了軟件包兩次:
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())]))
importutils
def does_module_exist_at_given_path(module_name, path):
'''
imp.find_module() does not find zipped eggs.
Needed for check: check if a package is installed twice.
'''
for path_item in path:
result=None
try:
result=imp.find_module(module_name, [path_item])
except ImportError:
pass
if result:
return bool(result)
if not os.path.isfile(path_item):
continue
# could be a zipped egg
module=zipimport.zipimporter(path_item).find_module(module_name)
if module:
return bool(module)
raise ImportError(module_name)
相關:imp.find_module() which supports zipped eggs
這個問題可以幫助你與這樣的:HTTP ://stackoverflow.com/questions/11166014/how-to-tell-if-you-have-multiple-djangos-installed – Ryan
他們是不同的版本?是一個安裝globablly和一個安裝在你的virtualenv? – dm03514
@guettli真的很可惜,你不能重現它。 –