<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by XYZ on Medium]]></title>
        <description><![CDATA[Stories by XYZ on Medium]]></description>
        <link>https://medium.com/@x_y_z?source=rss-8c79ad2edb3d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lkADYK_VCmlYiVAGB61q1A.png</url>
            <title>Stories by XYZ on Medium</title>
            <link>https://medium.com/@x_y_z?source=rss-8c79ad2edb3d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:52:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@x_y_z/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Compile Python as a macOS universal library (or Framework)]]></title>
            <link>https://medium.com/@x_y_z/compile-python-as-a-macos-universal-binary-ccdd605f3333?source=rss-8c79ad2edb3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/ccdd605f3333</guid>
            <category><![CDATA[universal]]></category>
            <category><![CDATA[cpython]]></category>
            <category><![CDATA[macos]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[cmake]]></category>
            <dc:creator><![CDATA[XYZ]]></dc:creator>
            <pubDate>Mon, 06 Jan 2025 09:06:55 GMT</pubDate>
            <atom:updated>2025-01-07T23:52:04.595Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7jDSwjDgQta_mLF3Nw9i3Q.jpeg" /><figcaption>Apollo versus Python, or the greater battle between order and chaos — as in Ovid’s Metamorphose (which I prefer for this example over the Greek version, as it’s as brutal as the developer experience gets).</figcaption></figure><h3>The problem(s)</h3><p>Here are some classical cases where you need your own Python on macOS:</p><ol><li>You can’t rely (or you don’t want to “pollute”) the default system Python that ships with macOS with the tons of modules you will likely install.</li><li>You can’t rely on the ones that can be installed via the package manager (HomeBrew). Recent Python versions on brew are NOT universal binaries. And when you’re building or running a C/C++ program as a universal binary, you need to ensure that <strong>all dependencies</strong> — including libraries and any other linked binaries — are also built as universal binaries.</li><li>You need to make your own UI .app application which needs a custom Python Framework.</li></ol><p>So, let’s compile <em>cpython</em> (the reference implementation of Python, written in C) as a universal binary on macOS, with <em>libpython</em> as a <strong>shared</strong> (<em>non-static</em>) <strong>universal library</strong>.</p><blockquote>A <strong>universal binary</strong> is designed to run on multiple architectures, such as both Intel (x86_64) and Apple Silicon (arm64) on macOS.</blockquote><blockquote>A <strong>static </strong>library is linked directly into an application at compile time, becoming part of the executable, while a <strong>dynamic </strong>(or <strong>shared</strong>) library is loaded at runtime, allowing multiple programs to share the same library in memory.</blockquote><h3>The solution</h3><h4>Organization</h4><p>You should always build and install in a separate folder to keep the source repo clean (this is called “out-of-source” build). In this case:</p><pre>~/Dev/cpython-grp<br>├── build<br>├── install<br>└── cpython</pre><h4>Preparation &amp; Git</h4><pre>mkdir -p ~/Dev/cpython-grp/{build,install}</pre><pre>cd ~/Dev/cpython-grp<br>export PYVER=3.10<br>git clone --branch $PYVER https://github.com/python/cpython.git<br>cd ~/Dev/cpython-grp/build</pre><h4>Configure (dynamic library)</h4><pre>../configure --prefix=../install \<br>             --enable-universalsdk=`xcrun --sdk macosx --show-sdk-path` \<br>             --with-universal-archs=universal2 \<br>             --enable-shared</pre><h4>Configure (static library)</h4><pre>../configure --prefix=../install \<br>             --enable-universalsdk=`xcrun --sdk macosx --show-sdk-path` \<br>             --with-universal-archs=universal2</pre><blockquote>There are more options and optional dependencies that vary between python versions. You can read more <a href="https://devguide.python.org/getting-started/setup-building/">https://devguide.python.org/getting-started/setup-building</a>/ and of course by running ./configure -h.</blockquote><h4>Configure (Framework)</h4><p>A <strong>macOS framework</strong> is a collection of resources bundled together to provide reusable functionality for applications on macOS. It is essentially a modular package that can contain libraries, APIs, documentation, interface elements, and other resources designed to facilitate software development.</p><pre>../configure --prefix=../install \<br>             --enable-universalsdk=`xcrun --sdk macosx --show-sdk-path` \<br>             --with-universal-archs=universal2 \<br>             --enable-framework=../Frameworks</pre><blockquote>Also, read <a href="https://github.com/python/cpython/blob/main/Mac/README.rst">https://github.com/python/cpython/blob/main/Mac/README.rst</a>, to know more about building a Framework, in case you need to ship a custom python in your own custom macOS .app application.</blockquote><h4>Make</h4><pre>make -j`expr $(nproc) / 2`</pre><h4>Install</h4><pre>make altinstall</pre><h4>Verify</h4><blockquote>From now on, we will assume a dynamic library was made.</blockquote><pre>$ lipo -archs ~/Dev/cpython-grp/install/lib/libpython3.10.dylib<br><br>&gt; x86_64 arm64</pre><p>Boom! 💥 Now you can use it (ab)use it, but make sure to avoid mixing Python environments:</p><ul><li>Use the correct pyton3 and pip3 from the right PATH</li><li>Set DYLD_LIBRARY_PATH to locate the shared libpython* library.</li><li>Configure PYTHONPATH to point to the site-packages for this python installation.</li></ul><blockquote>If you are into virtual environments, you can use the built-in venv module (python -m venv) to create your env.</blockquote><h4>Bonus: use it in CMake</h4><p>If you build your app with CMake and use this custom python version as a dependency, remember to set the following environment variables to ensure a conflict-free and correct environment (yes, I found cases where this does not work within a venv, so better safe than sorry).</p><ul><li>Python3_EXECUTABLE : set to the path to the interpreter.</li><li>Python3_LIBRARY : set to the path to the libpython* library.</li><li>Python3_INCLUDE_DIR : set to the path to the directory of the Python headers.</li></ul><h4>Challenge</h4><p>If you need a challenge, try to notarize the Framework with Apple notarization services. If I have time, I will write another article as it would comply with the write not-to-forget purpose of this blog.</p><p>That’s it.</p><p>Hope it helps someone else besides my future self.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ccdd605f3333" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>