When I start Swift from the command line I get the following error:
$ swift
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 52, in <module>
import weakref
File "/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Welcome to Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.39.1). Type :help for assistance.
The problem goes away when uninstalling homebrew's python@2 package, but that's not a true solution.
I mentioned this when Homebrew was still in the process of its Python 3/forced upgrade of Python 2 migration. Essentially, the issue is that LLDB requires the system version of Python 2 that comes with macOS, and will have issues with any other version. Unfortunately, due to the way the Homebrew maintainers have decided to move forward with this migration process, they've made it all but necessary to install their version of Python (which overrides the system's copy of Python) causing the issue you see here. They don't seem to see it as a major issue, so the best thing we have until now is the workaround citied in the thread I linked
I'm nitpicking, but you don't need to change PATH globally for this to work:
% which python
/usr/local/bin/python
% PATH="/usr/bin:$PATH" swift
Welcome to Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.39.1). Type :help for assistance.
1> ^D
% which python
/usr/local/bin/python
Your fix_python function could be as simple as:
function fix_python() {
PATH="/usr/bin:$PATH" $*
}
A similar error occurs if Python from MacPorts is installed:
$ which python
/opt/local/bin/python
$ swift
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python/lldb/__init__.py", line 98, in <module>
import six
ImportError: No module named six
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'run_one_line' is not defined
... (more than 1000 lines omitted) ...
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'run_one_line' is not defined
Welcome to Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2). Type :help for assistance.
1>
I am interesting to use swift REPL with more modern python2 from the brew (just for fun). The most problem was solved by
pip2 install six
Now there is only one error:
$ command swift
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 52, in
import weakref
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/weakref.py", line 14, in
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Welcome to Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1). Type :help for assistance.
1>
Is this considered a bug in Swift, a bug in how macOS handles installing multiple versions of the same binary, or just a hazard of installing multiple versions of the same binary?