Skip to content

Commit

Permalink
don't load site-packages in pdm run
Browse files Browse the repository at this point in the history
Close #708
  • Loading branch information
frostming committed Nov 1, 2021
1 parent b22e79c commit 4adbde4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions news/708.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Isolate the project environment with system site packages in `pdm run`, but keep them seen when PEP 582 is enabled.
2 changes: 2 additions & 0 deletions pdm/cli/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions pdm/pep582/sitecustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions tests/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4adbde4

Please sign in to comment.