pythonmacossetuptoolspkg-resourcesxattr

xattr: ImportError: No module named pkg_resources


I was trying to run xattr on macOS Catalina 10.15.2 and the error occurred:

Traceback (most recent call last):
  File "/usr/bin/xattr", line 8, in <module>
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

So I tried the solution given by @cwc and installed the latest version of setuptools

pip install -U setuptools

Requirement already up-to-date: setuptools in ./opt/anaconda3/envs/general/lib/python3.7/site-packages (45.1.0)

I also installed setuptools using pip3

pip3 install -U setuptools

Requirement already up-to-date: setuptools in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (45.1.0)

Besides, I tried the methods above in different conda environments with Python 3.6.10 and 3.7.4, but the problem was not solved. Any ideas as to this problem?


Solution

  • xattr as included in Catalina uses the system-installed Python 2.7. (As shown by the head command below.) So you need to make sure that version is set up correctly.

    $ head /usr/bin/xattr
    #!/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
    

    Make sure python is using the system default version:

    $ which python
    /usr/bin/python
    

    Then, run the following commands

    $ python -m ensurepip --default-pip
    $ python -m pip install --upgrade pip setuptools wheel
    $ python -m pip install --upgrade xattr==0.6.4
    $ /usr/bin/xattr
    

    Using pip in this way means that the the module is being run from site-packages directly, and will not clobber any other pip on your system (i.e. from Python 3.7)