ahupp / python-magic

A python wrapper for libmagic
Other
2.65k stars 283 forks source link

added path for local homebrew installation #267

Closed manvento closed 2 years ago

manvento commented 2 years ago

I wrote this little fix in order to access libmagic in mac even if it's installed with brew on user's home folder.

ahupp commented 2 years ago

If you set this in your environment:

export DYLD_LIBRARY_PATH=$HOME/.brew/lib

does that solve the problem? I'm hoping to move away from lots of platform-specific lib search paths, and for this one in particular I'd have to look more closely to see if it's a security issue.

manvento commented 2 years ago

Hi Adam,

Thank you for the response. Unfortunately, it doesn’t work.

I’ve configured the export variable, as you wrote, but the error is still present: File "/Users/manuele.ventoruzzo/miniforge3/envs/geocallai/lib/python3.9/site-packages/magic/loader.py", line 49, in load_lib raise ImportError('failed to find libmagic. Check your installation') ImportError: failed to find libmagic. Check your installation

I used the last version of python-magic available on pip: 0.4.27

Kind regards, Manuele.

manvento commented 2 years ago

Hi @ahupp,

Did you read my reply? How can we do it? With the environment variable, as you wrote, does not work. Could you approve the change that I proposed?

Thank you, Manuele.

BobDotCom commented 2 years ago

I'm having this issue as well. A fix would be great :)

ahupp commented 2 years ago

@manvento What are the contents of ~/.brew/lib? If libmagic does not appear there, does it appear after running brew link libmagic?

In the next breaking change I'd like to remove the homebrew-specific bits in magic/loader entirely, since the glob for ../Cellar/* is better handled with using brew link and standard paths. This just isn't the right layer to solve the problem. Also, AFAICT, ~/.brew isn't a standard path for homebrew installations and those are only fully-supported in the default /usr/local location.

BobDotCom commented 2 years ago

@ahupp I use the path ~/homebrew instead of ~/.brew, but it shouldn't really make any difference. In my case I do see libmagic present in the /lib subdir. However, setting the dyld path as you mentioned has no effect. Brew link has no effect.

ahupp commented 2 years ago

I did test and DYLD_LIBRARY_PATH worked, but according to this thread it can be stripped unpredictably:

https://github.com/Homebrew/brew/issues/13481#issuecomment-1168598570

I guess the solution then would be to run brew --prefix at import time and search there. Then I (hopefully!) can get rid of the other special cases for homebrew.

manvento commented 2 years ago

@manvento What are the contents of ~/.brew/lib? If libmagic does not appear there, does it appear after running brew link libmagic?

In the next breaking change I'd like to remove the homebrew-specific bits in magic/loader entirely, since the glob for ../Cellar/* is better handled with using brew link and standard paths. This just isn't the right layer to solve the problem. Also, AFAICT, ~/.brew isn't a standard path for homebrew installations and those are only fully-supported in the default /usr/local location.

@ahupp , it's already present.

I think that the idea to use brew --prefixat import time can be the better guess for this issue.

manvento commented 2 years ago

@ahupp , I've done the modify you proposed. Now the program checks if the brew --prefix command exists and, if so, adds the result to the variable paths. What do you think? It can be merged into the main repo?

ahupp commented 2 years ago

Ok, that looks good. Thanks! I'm not in front of a Mac right now but will give it some more testing when I get home.