mikez / spotify-folders

Get your Spotify folder hierarchy with playlists into JSON.
MIT License
60 stars 6 forks source link

AttributeError: module 'snappy' has no attribute 'uncompress' #12

Closed gabriel-roth closed 6 months ago

gabriel-roth commented 6 months ago

I've installed python-snappy using Homebrew but now I'm getting this error: AttributeError: module 'snappy' has no attribute 'uncompress'

Any idea what the issue could be?

mikez commented 6 months ago

@gabriel-roth Could it be related to this?

Otherwise, what do you see as a result of running:

pip show python-snappy
gabriel-roth commented 6 months ago

I don't think so — the output from the pip show command is

Name: python-snappy
Version: 0.7.1
Summary: Python library for the snappy compression library from Google
Home-page: http://github.com/intake/python-snappy
Author: Andres Moreira
Author-email: andres@andresmoreira.com
License: BSD
Location: /opt/homebrew/lib/python3.11/site-packages
Requires: cramjam
Required-by: 
mikez commented 6 months ago

@gabriel-roth That looks good.

Let's try something else in the shell:

/usr/bin/env python
>>> import snappy
>>> dir(snappy)

what does this output?

gabriel-roth commented 6 months ago

['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

mikez commented 6 months ago

@gabriel-roth Hmm, that looks less good.

Can you try typing

>>> snappy.__file__

and then paste the contents the file said path points towards?

gabriel-roth commented 6 months ago

Weirdly, that command has no output.

(Many thanks for your time and attention here btw.)

mikez commented 6 months ago

@gabriel-roth How about snappy.__path__ ?

gabriel-roth commented 6 months ago

That outputs _NamespacePath(['/opt/homebrew/Cellar/snappy'])

At that path, there's a folder (snappy) that contains another folder (1.1.10) which contains a couple more folders (include and lib) and various readme-type files.

mikez commented 6 months ago

Odd; since that seems like a non-Python folder. It's the contents of brew snappy which is a binary and dylib.

You might check two things:

>>> import sys
>>> print(sys.path)

and also the content of

/opt/homebrew/lib/python3.11/site-packages/snappy/

gabriel-roth commented 6 months ago

The first command returns: ['', '/Users/gabrielroth', '/opt/homebrew/Cellar/snappy', '/opt/homebrew/Cellar', '/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python312.zip', '/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12', '/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload', '/opt/homebrew/lib/python3.12/site-packages']

The folder at that path contains a folder called __pycache__ and four files: snappy.py, snappy_formats.py, __main__.py, and __init__.py

mikez commented 6 months ago

I notice that your path contains lots of Python 3.12 whereas your pip pointed towards Python 3.11. What does this say?

/usr/bin/env python --version
gabriel-roth commented 6 months ago

The output is Python 3.12.2. (I had to use python3 instead of python.)

I assume that means snappy is installed in Python 3.11? Is there a (reasonably) simple way to fix this?

mikez commented 6 months ago

Two more questions:

gabriel-roth commented 6 months ago

Yes, asking for python --version gives me a env: python: No such file or directory error.

I typically run spotifyfolders from Keyboard Maestro as part of a macro that does some other stuff, but for testing purposes I've got the folders.py file open in BBEdit and I'm using the Run command. I now realize that at some point I changed the shebang line at the top from #!/usr/bin/env python to #!/usr/bin/env python3.

mikez commented 6 months ago

Ok, in that case try this:

/usr/bin/env python3 -m pip install python-snappy
gabriel-roth commented 6 months ago

That gives me:

**error: externally-managed-environment**

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-brew-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.

    If you wish to install a non-brew packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
mikez commented 6 months ago

Oh, I see... did you recently upgraded to a newer Apple computer?

In that case, I have two ideas:

  1. Use this shebang: #!/usr/bin/env python3.11
  2. Set up a virtual environment as suggested by the error message and run your Keyboard Maestro commands using that virtual environment.
gabriel-roth commented 6 months ago

Yes! Changing the shebang as you suggested works. Thanks so much for going down this rabbit hole with me, and for this extremely useful script.