Linking to system python lib fails in Monterey

Code that compiled fine with XCode 12 / Big Sur fails on XCode 13/Monterey with the linker error:

"cannot link directly with dylib/framework, your binary is not an allowed client of /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.0.sdk/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.tbd for architecture x86_64"

What is causing this? How can one link to the system Python2 framework? What changed in Monterey?

Where you able to resolve this issue?

In my case,it presents like this: "

Cannot link directly with dylib/framework, your binary is not an allowed client of /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks//Python.framework/Python.tbd for architecture x86_64 " on Monterey. xcode 13.2.1(13C100).

if the project run on Big Sur and xcode 13.0.0, nothing occur. the project build success.

How to sovle this issue?anybody known?

i resolve it. i use cocoapods to manage my private libraries. one of the libraries had dependency python lead to the error message. Excluded it then success. on xcode 13.0 or earlier there are no error. but on xcode 13.1-13.2 will error. Are thre xcode change the path of python.framework?

You should rather link with the framework provided by your installed version of Python3.

Locate your installation of Python3 by using this command in Terminal: ls -la $(which python3). The framework is in the "Frameworks" folder located one level above "bin", for example: /usr/local/Cellar/python@3.9/3.9.7_1/Frameworks

Once your have the location of the Python3 framework, add it as a framework in your XCode project. In the Build Settings, don't forget to add:

  • the Frameworks folder location in Framework Search Path (e.g. "/usr/local/Cellar/python@3.9/3.9.7_1/Frameworks")
  • the framework's Headers folder in Header Search Path (e.g. "/usr/local/Cellar/python@3.9/3.9.7_1/Frameworks/Python.framework/Headers")

A possible solution, although not obviously ideal, is to edit the system libpython2.7.tbd file (it's ascii) and edit the allowable-clients field, adding in the name of the executable or library you wish to link with Python2.7.

This is not ideal, as every update will overwrite this file.

How can one link to the system Python2 framework?

To fix the error, You can get the python framework by installing Python 2 Release.

Such as downloading 2.7.18 at: https://www.python.org/ftp/python/2.7.18/python-2.7.18-macosx10.9.pkg

Install Python package, and then add the /Library/Frameworks/Python.framework/ to project.

The framework embedded in the built App bundle will takes up about 359MB of disk space.

BTW, If you only want to use the Xcode Python raw library locally and don't want to consume such the large space, making a dynamic library from Python source code would be a better choice.

Such as downloading 2.7.18 at: https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz

./configure --enable-shared
make

After make is finished, drag the Python.dylib into the project.

Linking to system python lib fails in Monterey
 
 
Q