Skip to content

Upstream downstream patches for allowing to make a relocatable (standalone) Python installation #119696

Open

Description

@FFY00

Feature or enhancement

Proposal:

I am currently looking at https://github.com/indygreg/python-build-standalone/tree/main/cpython-unix

This would be the first step towards python.org possibly providing a binary build for Linux.

cc @jezdez @indygreg

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. vstinner commented on Nov 21, 2024

    @vstinner
    Member

    CPython maintainers are happy to accept patches and work with contributors to make CPython relocatable on Linux (and hopefully macOS too)

    So far, no pull request was proposed.

  2. FFY00 commented on Dec 7, 2024

    @FFY00
    MemberAuthor

    Maybe this should be a PEP?

    Perhaps, I think there are two parts of this work:

    1. Make sure the Python runtime works on relocatable builds
    2. Add support for relocatable builds in the build system

    Since I don't think we currently rely on the build prefix for path calculation, 1) should already be supported, and shouldn't require any major changes. Runtime issues relocatable builds hit are likely bugs anyway. Considering this, I think 1) shouldn't need a PEP, as long as we don't run into any major issues or design constrains while working on it.

    Regarding 2), I think that might need PEP. Though, if there are any build system improvements we can make to help minimize the patching required for projects like python-build-standalone, I think it should be okay to do so, but that can be evaluated on a case-by-case basis.


    CPython maintainers are happy to accept patches and work with contributors to make CPython relocatable on Linux (and hopefully macOS too)

    Yes. You can ping me to review such contributions.

    The stated goal of distributing standalone relocatable distributions via python.org is out of scope as put extra burden on the maintainers

    I don't think that's out of the picture, but it's out of scope for this issue, and it would require a PEP. It's something to maybe consider if the work covered in this issue is successful, but for now we should focus on bridging the gap between the upstream and dowstreams, given how critical to the Python ecosystem this use-case is.

  3. zooba commented on Dec 9, 2024

    @zooba
    Member

    Regarding 2), I think that might need PEP. Though, if there are any build system improvements we can make to help minimize the patching required for projects like python-build-standalone, I think it should be okay to do so, but that can be evaluated on a case-by-case basis.

    Nah, provided it doesn't change the default or break any existing options, we can add support to the build system like any other feature. I would like to be able to argue for certain changes to be treated as bugfixes, since they're important to users who build from source and so are very impactful beyond our maintenance period, but we certainly shouldn't need a PEP to introduce improvements.

  4. emmatyping commented on Feb 7, 2025

    @emmatyping
    Member

    I'm interested in helping to make a relocatable Linux build of CPython a supported build option. I went through the build script and patches and here are the patches I think should be applied or adopted in some way to enable that. Note this list does not include a number of patches which should probably be upstreamed related to macOS and Linux cross compiling. It also only includes patches targeting the latest CPython.

    • patch-make-testembed-nolink-tcltk.patch - This patch handles the situation where testembed and libpython link against Tcl/Tk when built statically. In a static build, this causes duplicate symbols, so testembed should not link against Tcl/Tk when libpython links to modules statically.

    • patch-python-link-modules-3.11.patch - This patch is targeted towards macOS, but I think it motivates changes more generally:

      # Also on macOS, the `python` executable is linked against libraries defined by statically
      # linked modules. But those libraries should only get linked into libpython, not the
      # executable. This behavior is kinda suspect on all platforms, as it could be adding
      # library dependencies that shouldn't need to be there.
      

      I think I would agree it would be better to link libraries against libpython rather than the python executable. This is a pretty significant change however, so I don't know if it is worth the risk, especially if we're just focusing on Linux for now.

    • patch-tkinter-3.13.patch - This patch updates how unix builds find Tcl/Tk libraries to search in a few more places, which seems useful for a portable build.

    • patch-ctypes-static-binary.patch - This patches resolves import ctypes fails with a statically linked interpreter due to dlopen() failure #81241 by simply not making the ctypes.pythonapi object available. I'm not totally sure what the best path forward here is, but perhaps just noting it is unavailable on static CPython builds in documentation is sufficient?

    • patch-configure-disable-stdlib-mod-3.12 and patch-pwd-remove-conditional.patch - These patches hack around not using the configure script's module enable/disable logic. The patch comments say:

      # Python 3.11 has configure support for configuring extension modules. We really,
      # really, really want to use this feature because it looks promising. But at the
      # time we added this code the functionality didn't support all extension modules
      # nor did it easily support static linking, including static linking of extra
      # libraries (which appears to be a limitation of `makesetup`). So for now we
      # disable the functionality and require our auto-generated Setup.local to provide
      # everything.
      

      So to me this seems like the work left to do is make sure that the module enable/disable logic and makesetup script support static linking. This also affects patch-checksharedmods-disable.patch, which disables the shared module check because of the above patches.

    • patch-test-embed-prevent-segfault.patch - This may be an issue with the BOLT optimization when CPython is compiled statically? This needs more investigation to figure out why there is a crash.

    I'll take a look at the more obvious ones (e.g. addressing #81241) as a start.

  5. geofft commented on Feb 11, 2025

    @geofft
    Contributor

    I think there's a couple of different definitions of "relocatable". For instance, patch-ctypes-static-binary seems to be specifically needed where the Python interpreter is fully statically linked (does not use the ELF interpreter, ld.so), but our current releases are dynamically linked (uses the systemwide ld.so) and so I think this patch is not needed. There are probably a couple of other patches in python-build-standalone/cpython-unix/ that aren't relevant for what python-build-standalone is doing these days.

    Another distinction is whether third-party libraries that are used by Python (zlib, Expat, Tcl/Tk, etc.) are statically linked into libpython or the Python binary (which is orthogonal to whether the Python binary itself uses ld.so or not), and if not, whether they're picked up from the system or from something shipped along with the relocatable Python installation. I think python-build-standalone could ship all of these third-party libraries as separate shared libraries, but it's just that because of its history as targeting a static single-file interpreter, it doesn't.

    Out of curiosity, @emmatyping, do you have some specific use case in mind for a relocatable Python?

  6. emmatyping commented on Feb 13, 2025

    @emmatyping
    Member

    I think there's a couple of different definitions of "relocatable".

    Very true! In my mind it is merely "if I take this install, and scp it to another machine, it should continue to work". Which implies a lot about runtime dependency and module loading (e.g. all dependent libraries are consistently loaded).

    For instance, patch-ctypes-static-binary seems to be specifically needed where the Python interpreter is fully statically linked (does not use the ELF interpreter, ld.so), but our current releases are dynamically linked (uses the systemwide ld.so) and so I think this patch is not needed.

    I'll check if this is the case, it would make sense it isn't needed.

    I think python-build-standalone could ship all of these third-party libraries as separate shared libraries, but it's just that because of its history as targeting a static single-file interpreter, it doesn't.

    I'm a little concerned ld.so.cache may cause problems here. In a scenario where libpython.so is embedded in a program, if the program loads any library of the same SONAME, the bundled dependent library wouldn't be used, which could cause errors about missing symbols if the system library is older than the one used for the relocatable Python.

    Out of curiosity, @emmatyping, do you have some specific use case in mind for a relocatable Python?

    I would love to see an official relocatable build of CPython for Linux for many reasons (not needing to build CPython myself to get a version my OS doesn't package, a more official/signed equivalent to python-build-standalone, etc.) . I realize working on this won't mean that happens, and that'd probably require a PEP, but I think it is generally useful if a relocatable Linux (and probably macOS as well) build is a supported build configuration.

  7. brettcannon commented on Nov 21, 2025

    @brettcannon
    Member

    Since https://github.com/python/prebuilt-cpython/ is now a thing and we are working towards relocatable, prebuilt binaries for releases, I think we should start working towards upstreaming relevant patches.

    I think for simple stuff it can be tied to this issue, but anything that might require discussions we should have separate issues.

  8. zanieb commented on Nov 21, 2025

    @zanieb
    Contributor

    I'm working on taking the parts from #119696 (comment) and turning them into patches that we could upstream astral-sh/python-build-standalone#861

  9. ofek commented on Nov 21, 2025

    @ofek
    SponsorContributor

    This is incredibly exciting! Thanks a lot for everyone's hard work up until now and for what comes next 🙇‍♂️

  10. added 5 commits that reference this issue on Oct 22, 2026
  11. added 3 commits that reference this issue on Nov 7, 2026
  12. added 2 commits that reference this issue on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions