Find all packages installed with easy_install/pip?

前端 未结 18 1238
醉话见心
醉话见心 2020-11-27 08:47

Is there a way to find all Python PyPI packages that were installed with easy_install or pip? I mean, excluding everything that was/is installed with the distributions tool

18条回答
  •  余生分开走
    2020-11-27 09:50

    As @almenon pointed out, this no longer works and it is not the supported way to get package information in your code. The following raises an exception:

    import pip
    installed_packages = dict([(package.project_name, package.version) 
                               for package in pip.get_installed_distributions()])
    

    To accomplish this, you can import pkg_resources. Here's an example:

    import pkg_resources
    installed_packages = dict([(package.project_name, package.version)
                               for package in pkg_resources.working_set])
    

    I'm on v3.6.5

提交回复
热议问题