Skip to content

Allow environment variable to specify full path to libmagic shared lib #315

@bbenne10

Description

@bbenne10

As it currently sits, python-magic does not allow the user to customize the full path to magic.

This is kind of okay on a linux host, as you can set LD_LIBRARY_PATH and ctypes.util.find_library will respect this. On MacOS though, this is not the case.

This makes using python-magic on a macOS host with a nix-managed development tree quite hard.
One needs to either symlink some files around (and know that nix might cleanup the path at any arbitrary garbage collection) or install magic via homebrew (which I personally do not use otherwise) because there's a fixed set of places to look in specified by hand.

It seems to me that something like the following might help (at the top of loader.py:

def _lib_candidates():
  user_set_magic_path = os.environ.get("PYTHON_MAGIC_SO_PATH")  # Maybe someone has a better name?
  if user_set_magic_path:
    yield find_lib(user_set_magic_path)

  yield find_library('magic')
  # ... rest of function body

I'm happy to put a PR together for this if someone can give me the word on what the environment variable should be called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions