Skip to content

gh-104612: Document linking embedders for several Python versions - #162498

Open
itamaro wants to merge 4 commits into
python:mainfrom
itamaro:embedder-stable-abi-docs
Open

itamaro wants to merge 4 commits into
python:mainfrom
itamaro:embedder-stable-abi-docs

Conversation

@itamaro

@itamaro itamaro commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Add a section to the embedding guide that explains, for Linux, that linking with pythonX.Y-config --ldflags --embed ties an application to one minor version even when it only uses the Limited API, and how a single binary can instead load libpython3.so at run time. Mention renaming the dependency with patchelf as a packaging workaround, and list the limitations of both approaches.

Related discussion: https://discuss.python.org/t/on-embedders-and-stable-abi/110197

Add a section to the embedding guide that explains, for Linux, that
linking with `pythonX.Y-config --ldflags --embed` ties an application
to one minor version even when it only uses the Limited API, and how
a single binary can instead load `libpython3.so` at run time.
Mention renaming the dependency with `patchelf` as a packaging
workaround, and list the limitations of both approaches.

Related discussion: https://discuss.python.org/t/on-embedders-and-stable-abi/110197
@read-the-docs-community

read-the-docs-community Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

@zooba zooba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great! Well written, easy to follow, it's the kind of addition I've been meaning to make for Windows for years now 😄

Left a few comments to help the flow and clarity, but don't rely on me for strict correctness. I've done all of these things before, but I didn't reference my earlier work to check, and I don't do them often enough on Linux to remember the details.

Comment thread Doc/extending/embedding.rst Outdated
Comment on lines +417 to +419
This library defines no Python symbols itself.
It only depends on the versioned library, which the dynamic loader loads
along with it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This would probably flow better as something like "when the OS loads this library, it implicitly loads and references the symbols from the versioned library it is associated with" (IOW, a positive description of what happens, rather than 1.5 negative descriptions of what doesn't happen)

Comment thread Doc/extending/embedding.rst Outdated
Comment on lines +420 to +421
Linking an executable with ``-lpython3`` therefore fails with
``undefined reference`` errors, but an application can load

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I assume this is because the linker does not load transitive dependencies to bring their symbols into context, but is it safe to assume that everyone will assume that? Or should we be clearer (I'm thinking something like "when linking executables, the versioned library is not considered, and since -lpython3 doesn't include any symbols of its own, it will fail with undefined reference errors"- adjusted for correctness if I got anything wrong)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, that's accurate

Comment thread Doc/extending/embedding.rst Outdated
} py;

#define LOAD(lib, name) \
((*(void **)&py.name = dlsym(lib, "Py_" #name)) != NULL)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For an example, I'd remove the "Py_" and just spell it out each time. Removes an additional concept at a very small cost (and arguably an improvement - it helps search functions find references to the name when the name is complete).

Comment thread Doc/extending/embedding.rst Resolved
Comment thread Doc/extending/embedding.rst Resolved
Comment thread Doc/extending/embedding.rst Outdated
Comment on lines +481 to +482
$ readelf -d myapp | grep NEEDED
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These might be worth a comment (or move it after the note at line 486, or just remove it), as it's just inspecting the result to prove a point, and not actual code that people need to run.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

moving it after 486

Comment thread Doc/extending/embedding.rst Outdated
Comment on lines +497 to +501
* The dynamic loader looks for the versioned library by name in its usual
search path, such as :envvar:`!LD_LIBRARY_PATH` and the system library
directories, and not next to :file:`libpython3.so`.
To choose the exact file, pass the absolute path of the versioned library
to ``dlopen()`` instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The previous point says "use an absolute path" already, but I like it better here. Maybe merge the recommendation into this sentence and remove it from the first point?

Comment thread Doc/extending/embedding.rst Outdated
Comment on lines +502 to +503
* ``RTLD_GLOBAL`` makes Python's symbols available to the extension modules
that Python imports, which are normally not linked against a Python library.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah hah! There is a reason! I still think it's worth a one line comment in the code sample, I'm pretty sure this is esoteric knowledge that isn't clearly documented anywhere.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since it's now in the source, does it need to stay in the prose?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it's helpful to have it in both places. I don't want important information to show up only as an inline comment in an example.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or if there's somewhere in an "extending" section mentioning that it's an intentional part of how extension modules work, that would also suffice. As long as it's actually documented somewhere.

I don't like treating code samples as specification, so prefer seeing it in the prose as well, but I don't have a real strong opinion about where it should go.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

RTLD_GLOBAL allows extensions to not link to libpython. Not linking is the default on *nix, as that makes the extension compatible with debug builds of Python.

The Defining extension modules docs put build options out of scope. This kind of platform-specific esoteric knowledge was part of distlib, but moved out of core with the removal; bringing it all back as docs wouldn't be a small task.

Comment thread Doc/extending/embedding.rst Outdated
* ``RTLD_GLOBAL`` makes Python's symbols available to the extension modules
that Python imports, which are normally not linked against a Python library.

An existing application that calls the C API directly can instead be linked

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is where the alternative approach starts from, yes? Maybe bringing some of the warning up to here will help emphasize that we support everything above this point, and below this point is "at your own risk"?

At the very least, something that looks like a heading (or is a heading) will help avoid people reading this and thinking they need to keep going below here after they've done all the above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, I'll add a subtitle "Alternative: renaming the dependency after linking" (looks like a heading, not an actual heading because I didn't want it to show up in the navigation TOC)

not sure how strongly we want to call things here "supported" vs "unsupported", especially if later on we will want to reconsider cpython's stance about Stable ABI for embedders altogether.

I'll push one possible wording change, let me know what you think.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

not sure how strongly we want to call things here "supported" vs "unsupported"

Calling them "unsupported" now is fine 😉

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Calling them "unsupported" now is fine

updated

@ncoghlan ncoghlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! At a detailed level, I didn't have anything to add to @zooba's comments.

Comment thread Doc/extending/embedding.rst Outdated
Comment on lines +502 to +503
* ``RTLD_GLOBAL`` makes Python's symbols available to the extension modules
that Python imports, which are normally not linked against a Python library.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since it's now in the source, does it need to stay in the prose?

Comment thread Doc/extending/embedding.rst Outdated
Comment on lines +521 to +558
.. rubric:: Alternative: renaming the dependency after linking

Loading Python at run time, as described above,
is the preferred way to run one binary with several Python versions,
and it does not need anything described below.

The following is a packaging workaround for an existing application that
calls the C API directly and cannot easily be changed.
It is not recommended; use it at your own risk.

Such an application is linked as described :ref:`above <compiling>`.
The dependency recorded in the executable is then renamed, for example with
`patchelf <https://github.com/NixOS/patchelf>`__:

.. code-block:: shell-session

$ patchelf --replace-needed libpython3.12.so.1.0 libpython3.so myapp

.. warning::

Renaming the dependency does not make the application compatible with
other Python versions, and it has additional limitations and caveats:

* Renaming the dependency does not check that the application uses only
the Stable ABI.
Compile it with :c:macro:`Py_LIMITED_API` defined, as described in
:ref:`abi3-compiling`.
* If the application refers to a Python data symbol directly, for example
``&PyLong_Type``, the linker may have used a copy relocation.
This reserves space for the object in the executable, using its size in
the Python version that the application was linked against.
That size is not part of the Stable ABI and can differ between versions.
Look for such relocations with ``readelf -rW myapp | grep COPY``, and
compile the application as position-independent code (``-fPIC``) to
avoid them.
* The dynamic loader must find :file:`libpython3.so` in its search path
when the application starts.
Otherwise the application does not start at all.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a “you get to keep the pieces” solution; I'd be OK with leaving it out of the official docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

would you also be ok with keeping it? :)

this is the bit that directly addresses gh-104612, and I think it's useful to address it in the docs. it gives us an opportunity to discourage users from reaching to this approach

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If it's kept, I'd prefer to reduce it. Maybe something like “Note that libpython3.so is not meant for embedding and linking to it will not work, as noted [above] (link to A Python built with :option:`--enable-shared`…)”.

I don't think we need to discourage users patching their Python. It should be clear that they're on their own at that point.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I reduced the section

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review docs Documentation in the Doc dir needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants