我的 MacOS 终端不允许我在没有 sudo 的情况下执行 Python 命令

My MacOS Terminal doesn't allow me to execute Python commands without sudo

当我尝试从终端执行命令时,我不断收到错误描述:ImportError: / Symbol not found: / Expected in:。 但是,当我使用 sudo.

执行相同的命令时,不会出现该错误

例如:

$ pip search matplotlib

Returns:

Traceback (most recent call last):
  File "/opt/anaconda3/bin/pip", line 11, in <module>
    sys.exit(main())
  File "/opt/anaconda3/lib/python3.8/site-packages/pip/_internal/cli/main.py", line 73, in main
    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
  File "/opt/anaconda3/lib/python3.8/site-packages/pip/_internal/commands/__init__.py", line 104, in create_command
    module = importlib.import_module(module_path)
  File "/opt/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/opt/anaconda3/lib/python3.8/site-packages/pip/_internal/commands/search.py", line 18, in <module>
    from pip._internal.cli.req_command import SessionCommandMixin
  File "/opt/anaconda3/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 16, in <module>
    from pip._internal.index.package_finder import PackageFinder
  File "/opt/anaconda3/lib/python3.8/site-packages/pip/_internal/index/package_finder.py", line 21, in <module>
    from pip._internal.index.collector import parse_links
  File "/opt/anaconda3/lib/python3.8/site-packages/pip/_internal/index/collector.py", line 14, in <module>
    from pip._vendor import html5lib, requests
  File "/opt/anaconda3/lib/python3.8/site-packages/pip/_vendor/html5lib/__init__.py", line 28, in <module>
    from .serializer import serialize
  File "/opt/anaconda3/lib/python3.8/site-packages/pip/_vendor/html5lib/serializer.py", line 11, in <module>
    from xml.sax.saxutils import escape
  File "/opt/anaconda3/lib/python3.8/xml/sax/saxutils.py", line 6, in <module>
    import os, urllib.parse, urllib.request
  File "/opt/anaconda3/lib/python3.8/urllib/request.py", line 2619, in <module>
    from _scproxy import _get_proxy_settings, _get_proxies
ImportError: dlopen(/opt/anaconda3/lib/python3.8/lib-dynload/_scproxy.cpython-38-darwin.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
  Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
  Expected in: /opt/local/lib//libJPEG.dylib
 in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO

但是当相同的命令被 sudo 时,它起作用了:

$ sudo pip search matplotlib

Returns:

WARNING: The directory '/Users/ja_jagg/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

django-matplotlib (0.1)             - Matplotlib field for Django
matplotlib-stream (1.0.0)           - GCPDS: matplotlib stream
hangar-matplotlib (0.0.3)           - Matplotlib plugin for hangar
...

我在执行 ipython 和许多其他命令时遇到同样的错误:

ImportError: dlopen(/opt/anaconda3/lib/python3.8/lib-dynload/_scproxy.cpython-38-darwin.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
  Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
  Expected in: /opt/local/lib//libJPEG.dylib
 in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO

如果能帮助我理解此错误的原因以及如何修复它,我将不胜感激。我不得不 pip install 一些带有 sudo 的包,我知道这是非常糟糕的做法,我想避免它。谢谢。


编辑:

这个问题在安装Anaconda之前就存在了。 我最近安装了 Anaconda,并且长期使用 MacPorts。我在两个发行版中都安装了 Python 2 和 3。我已经遇到这个问题几年了,但直到现在才成为问题。

当使用与 Anaconda 不同的 IPython 路径时,我得到同样的错误:

$ which ipython
/Library/Frameworks/Python.framework/Versions/3.7/bin/ipython

$ ipython
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/bin/ipython", line 5, in <module>
    from IPython import start_ipython
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/__init__.py", line 55, in <module>
    from .terminal.embed import embed
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/terminal/embed.py", line 17, in <module>
    from IPython.terminal.ipapp import load_default_config
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/terminal/ipapp.py", line 28, in <module>
    from IPython.core.magics import (
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/magics/__init__.py", line 18, in <module>
    from .code import CodeMagics, MacroToEdit
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/magics/code.py", line 23, in <module>
    from urllib.request import urlopen
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 2583, in <module>
    from _scproxy import _get_proxy_settings, _get_proxies
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_scproxy.cpython-37m-darwin.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
  Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
  Expected in: /opt/local/lib//libJPEG.dylib
 in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO

在这两种情况下,重复元素都是文件 /opt/local/lib//libJPEG.dylib

当我ls文件时,它returns:

lrwxr-xr-x  1 root  admin  15 Jan 13  2020 /opt/local/lib//libJPEG.dylib -> libjpeg.9.dylib

据我所知,是文件的符号 link:

-rwxr-xr-x  1 root  admin  215064 Jan 13  2020 /opt/local/lib//libjpeg.9.dylib

由于命令与 sudo 一起工作正常,问题文件在 /opt/local/ 中,我想知道这个问题是否与 MacPorts 有任何关系。可能是没有足够权限的系统文件,或者损坏的 .dylib.

万一它可能有用:

$ echo $DYLD_LIBRARY_PATH 
/usr/local/mysql/lib/:/opt/local/lib/libgcc:/usr/OpenMotif/lib:/usr/local/gfortran/lib:/usr/local/gfortran/lib/i386:/opt/local/lib/:

明显的解决方案是卸载 MacPorts 并进行全新安装。但是,该选项会非常激烈,因为它会影响我需要定期访问的其他进程。我只是想确定这是否是一个合理的行动。

如果有任何反馈,我将不胜感激。感谢您的时间和关注!

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_scproxy.cpython-37m-darwin.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
  Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
  Expected in: /opt/local/lib//libJPEG.dylib

当您有一个不区分大小写的文件系统(这是 Mac 上的默认设置)并且您已将 DYLD_LIBRARY_PATH 设置为包含 libjpeg.dylib 的标准副本的目录(例如您在 /opt/local/lib 中从 MacPorts 获得的一个)。

Apple 的 ImageIO 框架包含 libjpeg.dylib 的非标准副本(Apple 已将其大写 libJPEG.dylib),其中包含标准 libjpeg.dylib 中不存在的其他符号——ImageIO 需要的符号。设置 DYLD_LIBRARY_PATH 告诉 OS 您希望它在您首先指定的路径中查找库,而不是在通常的路径中查找库。这与不区分大小写的文件系统相结合,意味着当 /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO 正在寻找 libJPEG.dylib 时,它会找到 /opt/local/lib/libJPEG.dylib 而不是 /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib.

解决方法是不设置DYLD_LIBRARY_PATH