Search code examples
pythonmacosimporterrorsudomacports

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


When I try to execute commands from the terminal, I keep getting an error with the description: ImportError: / Symbol not found: / Expected in:. However, that error doesn't appear when I execute the same command with sudo.

For example:

$ 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

But when the same command is sudoed, it works:

$ 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
...

I get the same error executing ipython and many other commands:

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

I would appreciate any help on understanding the cause of this error, and how can I fix it. I have had to pip install some packages with sudo, which I understand is very bad practice, and I want to avoid it. Thank you.


EDIT:

This issue has been present before installing Anaconda. I installed Anaconda recently, and have been a long time user of MacPorts. I have Python 2 and 3 installations in both distributions. I've been experiencing this issue for a few years, but hadn't been a problem until now.

When using a different IPython path from that in Anaconda, I get the same error:

$ 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

In both cases, the recurring element is th file /opt/local/lib//libJPEG.dylib.

When I ls the file, it returns:

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

which I understand, is a symbolic link for the file:

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

Since the command works fine with sudo, and the problem file is in /opt/local/, I wonder if this issue has anything to do with MacPorts. Perhaps a system file that doesn't have sufficient permissions, or a corrupted .dylib.

In case it may be useful:

$ 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/:

The apparent solution would be to uninstall MacPorts and do a clean install. However, that option would be quite drastic, as it would affect other processes that I need to access on a regular basis. I just want to make sure if that would be a reasonable action to take.

I would greatly appreciate any feedback on this. Thanks for your time and attention!


Solution

  • 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
    

    This happens when you have a case-insensitive filesystem (which is the default on Macs) and you have set DYLD_LIBRARY_PATH to a directory that contains a standard copy of libjpeg.dylib (such as the one you have in /opt/local/lib from MacPorts).

    Apple's ImageIO framework contains a nonstandard copy of libjpeg.dylib (which Apple has capitalized libJPEG.dylib) which contains additional symbols not present in a standard libjpeg.dylib—symbols which ImageIO requires. Setting DYLD_LIBRARY_PATH tells the OS that you would like it to look for libraries in the path(s) you specified first instead of where it would look for them normally. This, combined with your case-insensitive filesystem, means that when /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO is looking for libJPEG.dylib, it finds /opt/local/lib/libJPEG.dylib instead of /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib.

    The solution is not to set DYLD_LIBRARY_PATH.