Skip to content

Commit 8296b87

Browse files
committed
Don't list packages in current directory
1 parent 657cf25 commit 8296b87

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

news/7731.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid listing packages from current directory, when invoked as 'python -m pip list'

src/pip/_internal/commands/list.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import json
77
import logging
8+
import os
9+
import sys
810

911
from pip._vendor import six
1012
from pip._vendor.six.moves import zip_longest
@@ -139,12 +141,18 @@ def run(self, options, args):
139141

140142
cmdoptions.check_list_path_option(options)
141143

144+
# Filter sys.path, to avoid listing distributions
145+
# from current directory
146+
paths = options.path
147+
if paths is None:
148+
paths = [item for item in sys.path if item and item != os.getcwd()]
149+
142150
packages = get_installed_distributions(
143151
local_only=options.local,
144152
user_only=options.user,
145153
editables_only=options.editable,
146154
include_editables=options.include_editable,
147-
paths=options.path,
155+
paths=paths,
148156
)
149157

150158
# get_not_required must be called firstly in order to find and

tests/functional/test_list.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55

6+
from tests.lib import create_test_package_with_setup
67
from tests.lib.path import Path
78

89

@@ -543,3 +544,21 @@ def test_list_path_multiple(tmpdir, script, data):
543544
json_result = json.loads(result.stdout)
544545
assert {'name': 'simple', 'version': '2.0'} in json_result
545546
assert {'name': 'simple2', 'version': '3.0'} in json_result
547+
548+
549+
def test_list_skip_work_dir_pkg(script):
550+
"""
551+
Test that list should not include package in working directory
552+
"""
553+
554+
# Create a test package and create .egg-info dir
555+
pkg_path = create_test_package_with_setup(script,
556+
name='simple',
557+
version='1.0')
558+
script.run('python', 'setup.py', 'egg_info',
559+
expect_stderr=True, cwd=pkg_path)
560+
561+
# List should not include package simple when run from package directory
562+
result = script.pip('list', '--format=json', cwd=pkg_path)
563+
json_result = json.loads(result.stdout)
564+
assert {'name': 'simple', 'version': '1.0'} not in json_result

0 commit comments

Comments
 (0)