问题描述
在运行
from pyzbar.pyzbar import decode
的时候报错:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/var/folders/k0/4p98b7gn5cxc60_kvmtm31840000gn/T/ipykernel_59027/853365792.py in <module>
3
4 # Function used to decode QR Codes. Yuri reported that it doesn't import for Apple M1 computers
----> 5 from pyzbar.pyzbar import decode
~/opt/anaconda3/lib/python3.8/site-packages/pyzbar/pyzbar.py in <module>
5 from .locations import bounding_box, convex_hull, Point, Rect
6 from .pyzbar_error import PyZbarError
----> 7 from .wrapper import (
8 zbar_image_scanner_set_config,
9 zbar_image_scanner_create, zbar_image_scanner_destroy,
~/opt/anaconda3/lib/python3.8/site-packages/pyzbar/wrapper.py in <module>
137
138
--> 139 zbar_version = zbar_function(
140 'zbar_version',
141 c_int,
~/opt/anaconda3/lib/python3.8/site-packages/pyzbar/wrapper.py in zbar_function(fname, restype, *args)
134 """
135 prototype = CFUNCTYPE(restype, *args)
--> 136 return prototype((fname, load_libzbar()))
137
138
~/opt/anaconda3/lib/python3.8/site-packages/pyzbar/wrapper.py in load_libzbar()
113 global EXTERNAL_DEPENDENCIES
114 if not LIBZBAR:
--> 115 libzbar, dependencies = zbar_library.load()
116 LIBZBAR = libzbar
117 EXTERNAL_DEPENDENCIES = [LIBZBAR] + dependencies
~/opt/anaconda3/lib/python3.8/site-packages/pyzbar/zbar_library.py in load()
63 path = find_library('zbar')
64 if not path:
---> 65 raise ImportError('Unable to find zbar shared library')
66 libzbar = cdll.LoadLibrary(path)
67 dependencies = []
ImportError: Unable to find zbar shared library
解决方法
通过阅读报错信息,了解到需要安装zbar库。
Step1: 安装Homebrew
命令:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
因为本人的电脑已经安装过Homebrew,这一过程中会遇到的问题还需自行解决。安装过程通常需要比较长的时间。
Step2:用Homebrew安装zbar
brew install zbar
报错:
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/zbar-0.23.90_1.arm6
######################################################################## 100.0%
==> Installing dependencies for zbar: freetype, fontconfig, jbig2dec, jpeg, libidn, libtiff, little-cms2, openjpeg, ghostscript, aom, libde265, libffi, pcre, gdbm, mpdecimal, ca-certificates, openssl@1.1, readline, sqlite, xz, python@3.9, glib, docbook, docbook-xsl, gnu-getopt, xmlto, shared-mime-info, x265, libheif, liblqr, libomp, m4, libtool, imath, openexr, webp, imagemagick, jasper, dcraw and ufraw
==> Installing zbar dependency: freetype
==> Pouring freetype-2.11.0.arm64_big_sur.bottle.tar.gz
Error: No such file or directory @ rb_sysopen - /Users/ XXX/Library/Caches/Homebrew/downloads/4e2d36fc8776a66b63b4876891ef8bbe2012bde1b289c01bd7acdfd371220376--freetype-2.11.0.arm64_big_sur.bottle.tar.gz
解决方法:
brew update
Brew upgrade
然后再运行,一次不行再来一次,成功截图:
Step3:更改环境变量
open ~/.zshrc
把文件中anaconda的路径从:
export PATH="/opt/anaconda3/bin:$PATH")
改成
export PATH="/opt/homebrew/opt/python/libexec/bin:$PATH"
保存后使文件生效:
source ~/.zshrc
Step4:安装pyenv 版本控制工具
brew install pyenv
成功:
Step5:用pyenv安装Python 3.9:
pyenv install 3.9.10
把本地项目文件夹环境设置为Python3.9:
cd 到想设置的项目文件夹:
pyenv local 3.9.10
Step6:用Homebrew再次安装jupyter notebook:
brew install jupyter
jupyter notebook打开方式:
jupyter notebook