报错“
➜ demo pip3 install pvporcupinedemo
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
xyz, where xyz is the package you are trying to
install.
If you wish to install a Python library that isn't in Homebrew,
use a virtual environment:
python3 -m venv path/to/venv
source path/to/venv/bin/activate
python3 -m pip install xyz
If you wish to install a Python application that isn't in Homebrew,
it may be easiest to use 'pipx install xyz', which will manage a
virtual environment for you. You can install pipx with
brew install pipx
You may restore the old behavior of pip by passing
the '--break-system-packages' flag to pip, or by adding
'break-system-packages = true' to your pip.conf file. The latter
will permanently disable this error.
If you disable this error, we STRONGLY recommend that you additionally
pass the '--user' flag to pip, or set 'user = true' in your pip.conf
file. Failure to do this can result in a broken Homebrew installation.
Read more about this behavior here: <https://peps.python.org/pep-0668/>
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
➜ demo
这是由于系统环境的限制,阻止你使用 pip
在全局环境中安装 Python 包。为了避免对系统 Python 环境的影响,建议使用虚拟环境或 pipx
工具。以下是解决方法:
创建虚拟环境:
python3 -m venv path/to/venv
激活虚拟环境:
对于 macOS 和 Linux:
source path/to/venv/bin/activate
对于 Windows:
path\to\venv\Scripts\activate
在虚拟环境中安装你需要的包:
pip install pvporcupinedemo
pipx
安装 pipx
:
brew install pipx
pipx ensurepath
使用 pipx
安装你需要的包:
pipx install pvporcupinedemo
你也可以选择强制在系统环境中安装,但不推荐这样做,因为可能会破坏你的 Python 环境。如果你决定这么做,可以使用以下命令:
pip install pvporcupinedemo --break-system-packages
或者你可以修改 pip.conf
文件来永久允许这种行为:
mkdir -p ~/.config/pip
echo "[global]" >> ~/.config/pip/pip.conf
echo "break-system-packages = true" >> ~/.config/pip/pip.conf
为了确保你的 Homebrew 安装不被破坏,最好还是使用虚拟环境或 pipx
工具来管理 Python 包。