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

Isolate conda python from site packages, like venv does by default #24506

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions recipes/conda-create-no-user-site/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
BSD 3-clause license
Copyright (c) 2015-2022, conda-forge contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pathlib import Path

from conda import plugins
from conda.base.context import context
from conda.core.prefix_data import PrefixData


def action(argv: list) -> None:
target_prefix = context.target_prefix
python = PrefixData(target_prefix).get("python", None)
if python:
pyvenv_cfg = Path(target_prefix, "pyvenv.cfg")
with pyvenv_cfg.open("x", encoding="utf-8") as f:
f.write("include-system-site-packages = false\n")


@plugins.hookimpl
def conda_post_commands():
yield plugins.CondaPostCommand(
name="no-user-site",
action=action,
run_for={"create"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also consider adding install, remove and maybe even update. The reasons:

  • A user had created an environment without Python, but then installs something that brings Python in.
  • The user removes python from the environment, and there's a lingering pyvenv.cfg file, which should be removed if possible.

)
44 changes: 44 additions & 0 deletions recipes/conda-create-no-user-site/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package:
name: conda-create-no-user-site
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel a better name would be conda-pyvenv-compat if that's the focus? Or conda-no-user-site?

version: 0.1.0

source:
path: .
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the maintenance model for this package? Feedstocks are not usually equipped to deal with release processes and PRs, so maybe it's a better idea to commit this to a separate repo (maybe under conda-incubator, if you are inclined). I like seeing more conda plugins in the world and it would help with visibility.


build:
script: {{ PYTHON }} -m pip install . -vv
number: 0

requirements:
host:
- python
- pip
run:
- python
run_constrained:
- python >=3.3

test:
requires:
- python
commands:
- python -c "import sys, site; sys.exit(0 if site.ENABLE_USER_SITE==False else 1)"

about:
home: http://conda-forge.org/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
home: http://conda-forge.org/
home: http://github.com/conda-forge/conda-create-no-user-site-feedstock

summary: conda create will configure Python to ignore packages outside the environment
description: >
This package extends `conda create` to configure Python so it will ignore packages
installed with `pip install --user`. Envrionment creation will now add a
Copy link
Member

@jaimergp jaimergp Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
installed with `pip install --user`. Envrionment creation will now add a
installed with `pip install --user`. Environment creation will now add a

prefix-level "pyvenv.cfg" file with the line `include-system-site-packages = false`,
which gets read by Python's `site` module whenver Python is run. This aligns Python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
which gets read by Python's `site` module whenver Python is run. This aligns Python
which gets read by Python's `site` module whenever Python is run. This aligns Python

in conda environments with the default behavior of virtual environments created by
Python's `venv` module.
license: BSD-3-Clause
license_family: BSD
license_file: LICENSE.txt

extra:
recipe-maintainers:
- itcarroll
- mfansler
13 changes: 13 additions & 0 deletions recipes/conda-create-no-user-site/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "conda-create-no-user-site"
version = "0.1.0"
description = "Create a pyvenv.cfg in an environment created by conda."
requires-python = ">=3.8"
dependencies = ["conda"]

[project.entry-points.conda]
conda-create-no-user-site = "conda_create_no_user_site"