From 4adbde40dd2c25ce9921e1e5fab0732788eae50c Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Mon, 1 Nov 2021 16:36:55 +0800 Subject: [PATCH] don't load site-packages in pdm run Close #708 --- news/708.feature.md | 1 + pdm/cli/commands/run.py | 2 ++ pdm/pep582/sitecustomize.py | 9 +++++---- tests/cli/test_run.py | 10 ++++++++++ tests/conftest.py | 1 + 5 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 news/708.feature.md diff --git a/news/708.feature.md b/news/708.feature.md new file mode 100644 index 0000000000..34991dbcfa --- /dev/null +++ b/news/708.feature.md @@ -0,0 +1 @@ +Isolate the project environment with system site packages in `pdm run`, but keep them seen when PEP 582 is enabled. diff --git a/pdm/cli/commands/run.py b/pdm/cli/commands/run.py index 0cbd251150..c150abc83f 100644 --- a/pdm/cli/commands/run.py +++ b/pdm/cli/commands/run.py @@ -59,6 +59,8 @@ def _run_command( "PDM_PROJECT_ROOT": str(project.root), } ) + if not project_env.is_global: + os.environ["NO_SITE_PACKAGES"] = "1" if project_env.packages_path: os.environ.update({"PEP582_PACKAGES": str(project_env.packages_path)}) if env_file: diff --git a/pdm/pep582/sitecustomize.py b/pdm/pep582/sitecustomize.py index 921d84124a..0a56193453 100644 --- a/pdm/pep582/sitecustomize.py +++ b/pdm/pep582/sitecustomize.py @@ -77,10 +77,11 @@ def main(): # Second, add lib directories, ensuring .pth file are processed. site.addsitedir(libpath) - # Then add the removed path to the tail of the paths - known_paths.clear() - site.addusersitepackages(known_paths) - site.addsitepackages(known_paths) + if not os.getenv("NO_SITE_PACKAGES"): + # Then add the removed path to the tail of the paths + known_paths.clear() + site.addusersitepackages(known_paths) + site.addsitepackages(known_paths) main() diff --git a/tests/cli/test_run.py b/tests/cli/test_run.py index 23ecd7525c..54fb7a7318 100644 --- a/tests/cli/test_run.py +++ b/tests/cli/test_run.py @@ -29,6 +29,16 @@ def test_pep582_launcher_for_python_interpreter(project, invoke): assert output.decode().strip() == "2.24.0" +def test_pep582_with_system_site_packages(project, invoke): + env = os.environ.copy() + env.update({"PYTHONPATH": PEP582_PATH}) + proc = subprocess.run([project.python.executable, "-c", "import click"], env=env) + assert proc.returncode == 0 + + result = invoke(["run", "python", "-c", "import click"], obj=project) + assert result.exit_code != 0 + + def test_run_command_not_found(invoke): result = invoke(["run", "foobar"]) assert "Command 'foobar' is not found on your PATH." in result.stderr diff --git a/tests/conftest.py b/tests/conftest.py index c1c8301b41..58522d99e7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -251,6 +251,7 @@ def project_no_init(tmp_path, mocker): with temp_environ(): os.environ.pop("VIRTUAL_ENV", None) os.environ.pop("PEP582_PACKAGES", None) + os.environ.pop("NO_SITE_PACKAGES", None) pythonpath = os.environ.pop("PYTHONPATH", "") pythonpath = remove_pep582_path_from_pythonpath(pythonpath) if pythonpath: