@@ -12,7 +12,7 @@ def _lib_candidates():
1212
1313 paths = [
1414 here ,
15- os .path .abspath ("." ),
15+ os .path .abspath ('.' ),
1616 '/opt/local/lib' ,
1717 '/usr/local/lib' ,
1818 '/opt/homebrew/lib' ,
@@ -29,18 +29,23 @@ def _lib_candidates():
2929 # find_library searches in %PATH% but not the current directory,
3030 # so look for both
3131 yield os .path .join (here , '%s.dll' % i )
32- yield os .path .join (os .path .abspath ("." ), '%s.dll' % i )
32+ yield os .path .join (os .path .abspath ('.' ), '%s.dll' % i )
3333 yield find_library (i )
3434
3535 elif sys .platform == 'linux' :
36- # on some linux systems (musl/alpine), find_library('magic') returns None
37- yield subprocess .check_output (
38- "( ldconfig -p | grep 'libmagic.so.1' | grep -o '/.*' ) || echo '/usr/lib/libmagic.so.1'" ,
39- shell = True ,
40- universal_newlines = True ,
41- ).strip ()
42- yield os .path .join (here , 'libmagic.so.1' )
43- yield os .path .join (os .path .abspath ("." ), 'libmagic.so.1' )
36+
37+ prefixes = ['libmagic.so.1' , 'libmagic.so' ]
38+
39+ for i in prefixes :
40+ yield os .path .join (here , i )
41+ yield os .path .join (os .path .abspath ('.' ), i )
42+ # on some linux systems (musl/alpine), find_library('magic') returns None
43+ # first try ldconfig with backup string in case of error
44+ yield subprocess .check_output (
45+ "( ldconfig -p | grep '{0}' | grep -o '/.*' ) || echo '/usr/lib/{0}'" .format (i ),
46+ shell = True ,
47+ universal_newlines = True ,
48+ ).strip ()
4449
4550 yield find_library ('magic' )
4651
@@ -53,9 +58,7 @@ def load_lib():
5358 continue
5459 try :
5560 return ctypes .CDLL (lib )
56- except OSError as exc :
61+ except OSError : # file not found
5762 pass
58- else :
59- # It is better to raise an ImportError since we are importing magic module
60- raise ImportError ('failed to find libmagic. Check your installation' )
63+ raise ImportError ('failed to find libmagic. Check your installation' )
6164
0 commit comments