DEV Community

Chan
Chan

Posted on • Edited on

Error: externally-managed-environment on python package installation

Symptoms

I attempted to install a Python package using this command.

pip3 install [package-name]
Enter fullscreen mode Exit fullscreen mode

And then this error was thrown.

Error message Summary

Error message description

Description

  • Two types of Python are used in a single MacOS machine - User-specific and System-wide packages.
  • System-wide packages are shared by all the users, and even system package managers like apt. Installing user-specific packages as system-wide packages is very dangerous because it can conflict with system package managers if they run some Python scripts.

Solution

  1. Create ~/.config/pip/pip.conf and type this script.

    [install]
    break-system-packages = true
    user = true
    
  2. Install pipenv to manage dependencies per project. The pip.conf file lets the command run with --break-system-packages and --user options. Otherwise, pipenv can't install some dependencies since the python packages require a certain python version.

    pip3 install pipenv
    which python3.13
    pipenv --python /opt/homebrew/bin/python3.13(directory of python)
    
  3. If you're using zsh, add the directory of python to the PATH. Make sure the version is set explictly! Otherwise, pipenv command won't be found.

    export PATH="$HOME/Library/Python/3.13/bin:$PATH" # This loads 
    python
    
  4. Run a file with the all project dependencies in a virtual environment

    pipenv run python [file-name] .py
    
  5. Otherwise, you could run up a virtual environment instance and run the python file.

    pipenv shell
    python [file-name].py
    

Top comments (0)