Skip to content

Commit feac595

Browse files
committed
Don't use cwd in python -m pip command
1 parent 657cf25 commit feac595

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

news/7731.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid using the current directory for check, freeze, install, list and show commands, when invoked as 'python -m pip <command>'

src/pip/__main__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
import os
44
import sys
55

6+
# Remove '' and current working directory from the first entry
7+
# of sys.path, if present to avoid using current directory
8+
# in pip commands check, freeze, install, list and show,
9+
# when invoked as python -m pip <command>
10+
if sys.path[0] in ('', os.getcwd()):
11+
sys.path.pop(0)
12+
613
# If we are running from a wheel, add the wheel to sys.path
714
# This allows the usage python pip-*.whl/pip install pip-*.whl
815
if __package__ == '':

tests/functional/test_list.py

Lines changed: 42 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,44 @@ 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
565+
566+
567+
def test_list_include_work_dir_pkg(script):
568+
"""
569+
Test that list should include package in working directory
570+
if working directory is added in sys.path
571+
"""
572+
573+
# Create a test package and create .egg-info dir
574+
pkg_path = create_test_package_with_setup(script,
575+
name='simple',
576+
version='1.0')
577+
578+
script.run('python', 'setup.py', 'egg_info',
579+
expect_stderr=True, cwd=pkg_path)
580+
581+
# Add PYTHONPATH env variable
582+
script.environ.update({'PYTHONPATH': pkg_path})
583+
584+
# List should include package simple when run from package directory
585+
result = script.pip('list', '--format=json', cwd=pkg_path)
586+
json_result = json.loads(result.stdout)
587+
assert {'name': 'simple', 'version': '1.0'} in json_result

0 commit comments

Comments
 (0)