我正在设置运行 Sonoma 的新 Macbook Air M1 Lapot。我已经成功下载了 Homebrew 并且似乎已经下载了 pip (但它可能位于错误的目录中)。但是当运行 pip3 install Django 时,我收到此错误消息:
python3 -m pip install Django ─╯
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.
不太确定从这里开始哪里,因为我尝试过brew install pipx 和 pipx install Django。如果pip下载到错误的路径有没有简单的方法解决这个问题?
谢谢!
我只是期待安装 Django。
使用
which pip3
显示路径为:
which pip3 ─╯
/opt/homebrew/bin/pip3
对于 mac m1 来说哪个应该是正确的?
pip3 --version
:
pip 24.0 from /opt/homebrew/lib/python3.12/site-packages/pip (python 3.12)
我猜这是冲突?
谢谢
这里没有冲突,一切都按预期进行。从 PEP668 开始,python 安装可以标记为
externally-managed
,这意味着它们是使用包管理器(如brew)安装的,这会阻止 pip
直接安装到它们,这样你就不会遇到混乱的两个包管理器(brew
和pip
)以某种方式管理同一Python安装的各个部分。
错误信息也给出了解决方案
使用此代码创建虚拟环境:
python3 -m venv path/to/venv
使用此代码安装 django 和您需要的所有其他依赖项:
source path/to/venv/bin/activate
python3 -m pip install xyz
当你想运行 python 脚本时:
source path/to/venv/bin/activate
python3 /path/to/script.py
请注意,在每个终端中,您只需运行
source ...
行一次