Home >Backend Development >Python Tutorial >How to Avoid the \'error: externally-managed-environment\' When Using pip3?

How to Avoid the \'error: externally-managed-environment\' When Using pip3?

DDD
DDDOriginal
2024-12-01 01:26:151090browse

How to Avoid the

Avoid "error: externally-managed-environment" While Using Pip 3

The error "error: externally-managed-environment" arises when attempting to install Python packages system-wide using pip install xyz, indicating an externally managed environment on Linux machines. To resolve this issue, consider the following:

Preferred Solution: Using a Virtual Environment

The best practice for installing Python libraries and applications is to isolate them in a Python virtual environment. This prevents interference with other system components.

  • Using Pipx for Applications:
    For applications, install pipx as a system package:

    apt install pipx
    pipx install some-python-application
  • Creating a Virtual Environment Yourself:
    Create a virtual environment using venv and install libraries within it:

    python -m venv my-venv
    my-venv/bin/pip install some-python-library

Alternative Solution: System-Wide Installation

In exceptional cases, if system-wide installation is necessary, consider the following options:

  • Using Pip's --break-system-packages Flag:
    Run pip install xyz with the --break-system-packages flag, but be aware that this may compromise system stability.
  • Adding to Pip's Configuration:
    Add the following lines to ~/.config/pip/pip.conf:

    [global]
    break-system-packages = true

The above is the detailed content of How to Avoid the \'error: externally-managed-environment\' When Using pip3?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn