pypa/pip

Missing " get_installed_distributions() " attribute in module " pip "

realJustinLee opened this issue ยท 13 comments

  • Pip version: 10.0.0
  • Python version: 3.6.5 && 2.7.14
  • Operating system: macOS 10.13.5

Description:

for dist in pip.get_installed_distributions():
AttributeError: 'module' object has no attribute 'get_installed_distributions'

What I've run:

pip.get_installed_distributions()

See https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program - importing functions from pip is not supported. In this case, setuptools (specifically pkg_resources) probably has the functionality you need.

@Great-Li-Xin I think what you're looking for is

pip._internals.get_installed_distributions()

but obviously it's a bad practice. And it's best to not use it, as mentioned by @pfmoore

@codingCoffee No, that's explicitly not the right thing to do. Please don't do this, we will not support such usage.

@pfmoore, I agree, that it's a bad practice. I just meant the comment as a quick fix to the problem

@08zhangyi thats horrible - pip has json output for that which gives correct parsed results, please edit/remove the bad code

@codingCoffee same for your bad example - because people do actually use bad examples because they are the first thing they find that works

Hi @Great-Li-Xin!

You can use pkg_resources.working_set for getting the same information. It's part of setuptools, which is probably already installed on your system. That is a supported way for getting the exact same information. Using pip's internal functions is not supported.

import pkg_resources

dists = [d for d in pkg_resources.working_set]
# Filter out distributions you don't care about and use.

The function @codingCoffee mentioned has moved into an pip._internal name, to signify that usage of the same is not supported and may change anytime without notice. It is strongly discouraged to use it.

@08zhangyi that seems to be a very fragile way to do this. As @RonnyPfannschmidt points out, there's JSON output pip list --format=json that is much better suited for consumption by computers.


@Great-Li-Xin feel free to close this issue if your concerns are addressed. :)

@pradyunsg , thank you! I have deleted my answer and changed my code as you say.

For pip 10 use from pip._internal.utils.misc import get_installed_distributions

For pip 10 use from pip._internal.utils.misc import get_installed_distributions

To restate, this function has moved into pip._internal namespace, to signify that usage of the same is not supported and may change anytime without notice. It is strongly discouraged to use it.

Downgrade pip to 9.0.3 will fix this problem

pip install pip==9.0.3

@Yemsheng Downgrading to pip 9.0.3 is a way to work around this issue; you're essentially pinning yourself to an older version of pip until the underlying project changes itself to use a supported way to perform these actions.

well i believe the suggested solution is best for me
So i can remove the tight couple with pip

import pkg_resources

dists = [d for d in pkg_resources.working_set]

lock commented

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.