解决MacOS系统Python项目导入pyzbar因找不到zbar而无法正常运行的问题

问题描述

报错:

    from pyzbar import pyzbar
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pyzbar/pyzbar.py", line 7, in 
    from .wrapper import (
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pyzbar/wrapper.py", line 151, in 
    zbar_version = zbar_function(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pyzbar/wrapper.py", line 148, in zbar_function
    return prototype((fname, load_libzbar()))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pyzbar/wrapper.py", line 127, in load_libzbar
    libzbar, dependencies = zbar_library.load()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pyzbar/zbar_library.py", line 65, in load
    raise ImportError('Unable to find zbar shared library')
ImportError: Unable to find zbar shared library

pyzbar 在window系统中,使用一切正常,

 pip install pyzbar

但当macos环境中使用pyzbar框架进行条码识别时,

 from pyzbar import pyzbar

看似引入包都没有问题,也不会报错,但是实际上在非Window系统中,pyzbar框架需要查找zbar的头文件,即为zbar框架的二次封装。参考以下pyzbar源码

if 'Windows' == platform.system():
        # Possible scenarios here
        #   1. Run from source, DLLs are in pyzbar directory
        #       cdll.LoadLibrary() imports DLLs in repo root directory
        #   2. Wheel install into CPython installation
        #       cdll.LoadLibrary() imports DLLs in package directory
        #   3. Wheel install into virtualenv
        #       cdll.LoadLibrary() imports DLLs in package directory
        #   4. Frozen
        #       cdll.LoadLibrary() imports DLLs alongside executable
        fname, dependencies = _windows_fnames()

        def load_objects(directory):
            # Load dependencies before loading libzbar dll
            deps = [
                cdll.LoadLibrary(str(directory.joinpath(dep)))
                for dep in dependencies
            ]
            libzbar = cdll.LoadLibrary(str(directory.joinpath(fname)))
            return deps, libzbar

        try:
            dependencies, libzbar = load_objects(Path(''))
        except OSError:
            dependencies, libzbar = load_objects(Path(__file__).parent)
    else:
        # Assume a shared library on the path
        path = find_library('zbar')
        if not path:
            raise ImportError('Unable to find zbar shared library')
        libzbar = cdll.LoadLibrary(path)
        dependencies = []

解决办法:

1、安装zbar

 brew install zbar

2、但是再次运行仍然无法找到,在

not support macbook pro m1? · Issue #3 · npinchot/zbar · GitHubbrew install zbar Error: Cannot install under Rosetta 2 in ARM default prefix (/opt/homebrew)! To rerun under ARM use: arch -arm64 brew install ... To install under x86_64, install Homebrew into /usr/local. Then : arch -arm64 brew instal...https://github.com/npinchot/zbar/issues/3 中看到回答:

这个解决方案为任何 Python 解释器解决了这个问题。

在终端执行如下两个命令

 $ mkdir ~/lib
 $ ln -s $(brew --prefix zbar)/lib/libzbar.dylib ~/lib/libzbar.dylib

使得pyzbar可以在默认的环境中找到libzbar,顺利解决。

你可能感兴趣的:(python,macos)