'Xcode Python - Too many levels of symbolic links

Was attempting to set up Xcode for python dev just because and I ran into an issue.

I followed this tutorial: Run Python Code in XCode 10! (2018) While the UI is a bit different in the initial creation of the project I was able to find what I was looking for.

path to python3: /usr/bin/python3

After following the instruction and running the code I ran into this error:

2021-08-10 14:20:54.227437-0500 python3[5828:364044] Failed to open macho file at /Library/Developer/CommandLineTools/usr/bin/python3 for reading: Too many levels of symbolic links
/Library/Developer/CommandLineTools/usr/bin/python3: can't open file 'testingPython.py': [Errno 1] Operation not permitted
Program ended with exit code: 2

I checked the /usr/bin dir and I didn't see anything representing a symlink for python3

lrwxr-xr-x     1 root   wheel        74 Jan  1  2020 pydoc -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc2.7
lrwxr-xr-x     1 root   wheel        74 Jan  1  2020 pydoc2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc2.7
lrwxr-xr-x     1 root   wheel        75 Jan  1  2020 python -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x     1 root   wheel        82 Jan  1  2020 python-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x     1 root   wheel        75 Jan  1  2020 python2 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x     1 root   wheel        75 Jan  1  2020 python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x     1 root   wheel        82 Jan  1  2020 python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
-rwxr-xr-x     1 root   wheel    137616 Jan  1  2020 python3
lrwxr-xr-x     1 root   wheel        76 Jan  1  2020 pythonw -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
lrwxr-xr-x     1 root   wheel        76 Jan  1  2020 pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7

so I decided to check Homebrew: brew info python3

and got this:

[email protected]: stable 3.9.5 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/[email protected]
License: Python-2.0
==> Dependencies
Build: pkg-config ✘
Required: gdbm ✘, mpdecimal ✘, [email protected] ✔, readline ✘, sqlite ✘, xz ✘
==> Caveats
Python has been installed as
  /opt/homebrew/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /opt/homebrew/opt/[email protected]/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /opt/homebrew/lib/python3.9/site-packages

tkinter is no longer included with this formula, but it is available separately:
  brew install [email protected]

See: https://docs.brew.sh/Homebrew-and-Python

it mentioned not being installed so I tried reinstalling and got a warning:

Warning: The post-install step did not complete successfully
You can try again using:
  brew postinstall [email protected]

So I tried the post-install and got the same response

So I went in and manually deleted all of Homebrew's symlinks for python3 from /opt/homebrew.bin and the entire folder /opt/homebrew/[email protected]

Ran build again and got the same error output

reinstalled python3: brew install python3

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
Updated 2 formulae.

==> Downloading https://ghcr.io/v2/homebrew/core/python/3.9/manifests/3.9.6
Already downloaded: /Users/dmconklin/Library/Caches/Homebrew/downloads/497333c6ca5bfe88a2fd95008a6b111ac766162ed5900db38c7529dbd33e2859--python@3.9-3.9.6.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.9/blobs/sha256:36bdb8151cdc242487b9d3e6d6f305057b5699b4f6ac6905fc875e11b4ac2f3b
Already downloaded: /Users/dmconklin/Library/Caches/Homebrew/downloads/19520443760763aaa669f5ffeafb108cb2ba3b2b10f674bc4dc179b05f30ef3f--python@3.9--3.9.6.arm64_big_sur.bottle.tar.gz
==> Pouring [email protected]_big_sur.bottle.tar.gz
Warning: This keg was marked linked already, continuing anyway
==> /opt/homebrew/Cellar/[email protected]/3.9.6/bin/python3 -m ensurepip
==> /opt/homebrew/Cellar/[email protected]/3.9.6/bin/python3 -m pip install -v --no-deps --no-index --upgrade --isolated --target=/opt/homebrew/lib/python3.9/site-packages /opt/homebrew/Cellar/[email protected]/3.9.6/Frameworks/Python
==> Caveats
Python has been installed as
  /opt/homebrew/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /opt/homebrew/opt/[email protected]/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /opt/homebrew/lib/python3.9/site-packages

tkinter is no longer included with this formula, but it is available separately:
  brew install [email protected]

See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
🍺  /opt/homebrew/Cellar/[email protected]/3.9.6: 3,080 files, 56.5MB

brew info python3

[email protected]: stable 3.9.6 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/opt/homebrew/Cellar/[email protected]/3.9.6 (3,080 files, 56.5MB) *
  Poured from bottle on 2021-08-10 at 15:29:26
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/[email protected]
License: Python-2.0
==> Dependencies
Build: pkg-config ✘
Required: gdbm ✔, mpdecimal ✔, [email protected] ✔, readline ✔, sqlite ✔, xz ✔
==> Caveats
Python has been installed as
  /opt/homebrew/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /opt/homebrew/opt/[email protected]/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /opt/homebrew/lib/python3.9/site-packages

tkinter is no longer included with this formula, but it is available separately:
  brew install [email protected]

See: https://docs.brew.sh/Homebrew-and-Python

and after that I still have the same error about too many levels of symbolic links

I'm not sure what else to do or where to look.

found:

lrwxr-xr-x   1 dmconklin  admin      8 Jun 28 03:57 2to3 -> 2to3-3.9
-rwxr-xr-x   1 dmconklin  admin    169 Aug 10 15:29 2to3-3.9
lrwxr-xr-x   1 dmconklin  admin      7 Jun 28 03:57 idle3 -> idle3.9
-rwxr-xr-x   1 dmconklin  admin    167 Aug 10 15:29 idle3.9
-rwxr-xr-x   1 dmconklin  admin    247 Aug 10 15:29 pip3
-rwxr-xr-x   1 dmconklin  admin    247 Aug 10 15:29 pip3.9
lrwxr-xr-x   1 dmconklin  admin      8 Jun 28 03:57 pydoc3 -> pydoc3.9
-rwxr-xr-x   1 dmconklin  admin    152 Aug 10 15:29 pydoc3.9
lrwxr-xr-x   1 dmconklin  admin      9 Jun 28 03:57 python3 -> python3.9
lrwxr-xr-x   1 dmconklin  admin     16 Jun 28 03:57 python3-config -> python3.9-config
-rwxr-xr-x   1 dmconklin  admin  69168 Aug 10 15:29 python3.9
-rwxr-xr-x   1 dmconklin  admin   2111 Aug 10 15:29 python3.9-config

in: /opt/homebrew/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/bin

I tried to using that file path directly to python3.9 instead of python3 in /usr/bin/ and still get the same output in Xcode

Xcode Settings also tried this Xcode Settings

same result, too many levels of symlinks

also note that python3 isn't accessible when editing the scheme

/opt/homebrew/bin /opt/homebrew/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/bin



Solution 1:[1]

You're looking for solution in a wrong direction.

  1. /usr/bin/python3 never existed, even shipping of /usr/bin/python2 will be dropped out of macOS in a future macOS release. Installing python with pyenv or conda is recommended.

  2. You messed up Python in homebrew. Homebrew package python is designed to serve as a dependency for other brewed packages. The default linked python package should be python but not a versioned on like [email protected].

    brew unlink [email protected]
    brew install python
    brew link python # this may be unneeded, run it anyway
    

For your project, if you need python3, choose /opt/homebrew/bin/python3. But for a big project with many third-party python packages, use a virtual environment.

BTW, you guys code python with Xcode? Switch to VSCode or Pycharm.

Solution 2:[2]

I also met this problem. I created a link to python3 in /opt/homebrew/bin folder, and solved this alert.

cd /opt/homebrew/bin
ln python3 xcodePython3  // any name you'd like to use

Then in "Edit Scheme", select "Executable" to the /opt/homebrew/bin folder, and select xcodePython3.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MattDMo
Solution 2 Edwardai