Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different results between pip list and python -m pip list #7731

Closed
uranusjr opened this issue Feb 13, 2020 · 5 comments · Fixed by #7955, #7987 or #8012
Closed

Different results between pip list and python -m pip list #7731

uranusjr opened this issue Feb 13, 2020 · 5 comments · Fixed by #7955, #7987 or #8012
Labels
auto-locked Outdated issues that have been locked by automation C: list/show 'pip list' or 'pip show' state: awaiting PR Feature discussed, PR is needed type: bug A confirmed bug or unintended behavior

Comments

@uranusjr
Copy link
Member

uranusjr commented Feb 13, 2020

Environment

  • pip version: 19.0 or later (I didn’t test earlier versions)
  • Python version: any
  • OS: any

Description

From pypa/pipx#365. The result list would either include the package in the working directory or not, depending on how pip is invoked.

  • pip list would not contain the package in cwd.
  • python -m pip list would contain the package in cwd.

Expected behavior

The behaviour should be the same no matter how pip is invoked. Ideally the package in cwd should not be included (unless the package is also in sys.path other than .).

How to Reproduce

Create package metadata in cwd:

mkdir foo
cd foo
echo "from setuptools import setup; setup(name='foo', version='1.0.0')" > setup.py
python setup.py egg_info

Output

$ pip list
Package    Version
---------- -------
pip        19.0.3 
setuptools 40.8.0

$ python -m pip list
Package    Version
---------- -------
foo        1.0.0  
pip        19.0.3 
setuptools 40.8.0
@gutsytechster
Copy link
Contributor

From Python's official documentation for -m option

As with the -c option, the current directory will be added to the start of sys.path

But won't this behaviour need to be changed for every command pip invokes? If we could somehow skip the package if it is present in the cwd at the start of getting packages. Then, the packages provided to each of these commands would be just those which are not present in cwd.

I am not too familiar with pip's source code, I am diving into it to know more. Any kind of pointer will help.

@gutsytechster
Copy link
Contributor

On reading through the code, I found that all the commands that requires a list of installed distributions, calls the get_installed_distribution function in https://github.com/pypa/pip/blob/master/src/pip/_internal/utils/misc.py#L400, which in turn fetches the list from here https://github.com/pypa/pip/blob/master/src/pip/_vendor/pkg_resources/__init__.py#L553.

What I thought was to add a check like

if entries[0] == '':
    entries.pop(0)

after the line https://github.com/pypa/pip/blob/master/src/pip/_vendor/pkg_resources/__init__.py#L564, ie when entries is assigned sys.path. We can remove '' from the list of directories.

This works locally i.e. it does not consider the current directory. But I am unable to write a test for it as of now.

@pradyunsg pradyunsg added C: list/show 'pip list' or 'pip show' state: awaiting PR Feature discussed, PR is needed type: bug A confirmed bug or unintended behavior labels Mar 15, 2020
@triage-new-issues triage-new-issues bot removed the S: needs triage Issues/PRs that need to be triaged label Mar 15, 2020
@pradyunsg
Copy link
Member

The resolution for this would likely look very similar to my suggestion for fixing a similar issue in pip freeze #7810 (comment).

@deveshks
Copy link
Contributor

Hi @pradyunsg,

Can I take up this PR and try out the fix you mentioned in the comment above?

@sbidoul
Copy link
Member

sbidoul commented Apr 3, 2020

Noting this also applies to pip check (#7971).

@lock lock bot added the auto-locked Outdated issues that have been locked by automation label May 5, 2020
@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
auto-locked Outdated issues that have been locked by automation C: list/show 'pip list' or 'pip show' state: awaiting PR Feature discussed, PR is needed type: bug A confirmed bug or unintended behavior
Projects
None yet
5 participants