在初学 Python 会遇到一些安装新手问题

国内镜像

临时修改

pip 命令加上 -i 参数,指定 pip 源

pip install foo -i https://mirrors.aliyun.com/pypi/simple

永久修改

修改 pip.conf 文件,修改内容如下:

[global]

index-url=https://mirrors.aliyun.com/pypi/simple

各个系统 pip.conf 位置如下:

  • Linux: $HOME/.config/pip/pip.cof
  • Mac: $HOME/.config/pip/pip.conf
  • Windows: %APPDATA%\pip\pip.ini

镜像源

Pip 安装错误

在使用 PIP 安装模块会遇到多版本的问题

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try 'pacman -S
    python-xyz', where xyz is the package you are trying to
    install.

    If you wish to install a non-Arch-packaged Python package,
    create a virtual environment using 'python -m venv path/to/venv'.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.

    If you wish to install a non-Arch packaged Python application,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. Make sure you have python-pipx
    installed via pacman.

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.

解决办法是使用 venv:

python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -r requirements.txt

参考链接