GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — It should be possible for a Python environment to disable the "user site" functionality without patching the stdlib · Issue #99312 · python/cpython

It should be possible for a Python environment to disable the "user site" functionality without patching the stdlib #99312

New issue
Copy link
New issue
Copy link
Open
Open
It should be possible for a Python environment to disable the "user site" functionality without patching the stdlib#99312
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@njsmith
njsmith
opened on Nov 10, 2022
Issue body actions

The stdlib module site.py runs at startup, and is responsible for setting up a Python environment's basic search paths. There are various ways to customize this, e.g. you can drop in a sitecustomize.py file to add new search paths.

One thing site.py does by default is to add the "user site path" to sys.path, e.g. $HOME/.local/lib/python$VERSION/site-packages. This makes sense and is quite handy for global python installs where users want to add a few packages without the fuss of creating a venv: just pip install --user somepkg. And this is becoming more important as moves towards doing --user installs by default when outside a venv.

However, I think most people do not expect pip install --user somepkg will effectively inject somepkg into every venv that user ever creates. The whole point of creating a venv is to make an isolated environment. But, currently, site.py always adds the user site path to sys.path if possible, and there's no way to configure this through an envvar or sitecustomize.py – the only way to permanently disable it is to manually patch site.py in the stdlib. (Specifically: site.py runs addusersitepackages before it runs execsitecustomize, see here.) This leads to surprising behavior, or fragile hacks like modifying site.py with regexes.

There should be some way to create a Python environment where the user site path is disabled, without modifying the stdlib. (And venv should probably use it.)

One option would be to move execsitecustomize earlier in the setup, before addusersitepackages, so it has the option of doing import site; site.ENABLE_USER_SITE= False. I think the main downside here is that right now, if someone has a sitecustomize.py file in their user site path, it will no longer be executed. This is ... kind of fine? sitecustomize.py is supposed to be reserved for the python environment itself; user-specific customization is supposed to go in usercustomize.py. So e.g. this kind of setup is already broken if a distro ships a Python with a custom sitecustomize.py in it – which Ubuntu, for example, already does! And if it's a major problem we could even detect this case and warn about it. (E.g. after addusersitepackages make a second check for the existence of sitecustomize.py.)

Alternatively we could add some other kind of configuration file, earlysitecustomize.py or whatever. As long as it's something that's (a) on disk, so it's persistent for a given environment, and (b) can be dropped into a venv.

Activity

  1. njsmith
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Nov 10, 2022
  2. njsmith commented on Nov 10, 2022

    @njsmith
    njsmith
    on Nov 10, 2022
    ContributorAuthor
    More actions

    Doh, I'm dumb. I missed this bit of code that already does ENABLE_USER_SITE = False whenever we have a pyvenv.cfg with include-system-site-packages = false. So this is already solved for venv, it's just hard-coded to only work with venvs, not any other kind of isolated envs like conda or the thing I'm working on.

  3. merwok commented on Jan 22, 2023

    @merwok
    merwok
    on Jan 22, 2023
    Member
    More actions

    The generic options are command-line param -s or env var PYTHONNOUSERSITE; are these suitable for you?

  4. CAM-Gerlach commented on Jan 23, 2023

    @CAM-Gerlach
    CAM-Gerlach
    on Jan 23, 2023
    Member
    More actions

    not any other kind of isolated envs like conda

    Ah, so this is where that (very frustrating) user problem is always coming from...funny to see this come full circle.

    The generic options are command-line param -s or env var PYTHONNOUSERSITE; are these suitable for you?

    Neither of these appear to be a solution for either @njsmith 's use cases or conda's (or indeed, other redistributors), at least without patching (i.e. essentially the previous approach) because that is user-set and -controlled configuration, whereas distributor-controlled config is needed.

  5. pelson commented on Jul 27, 2023

    @pelson
    pelson
    on Jul 27, 2023
    Contributor
    More actions

    The stdlib module site.py runs at startup, and is responsible for setting up a Python environment's basic search paths

    FWIW, technically, this isn't quite true anymore (the site module runs, but it doesn't come from site.py), since site.py is deep-frozen at build time. I raised an issue for this in #107344.

    In my case, disabling user site-packages is definitely one use case. I also want to be able to add other site-package directories. I don't really want to be using sitecustomize (though, perhaps that is what it is indeed for), and want to be able to do this on an already built distribution. I would be happy if I don't need to patch the stdlib to do this though.

  6. iritkatriel
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.