Felipe's Blog

Your go-to source for Linux/Unix-like – tech – engineering.

Archive for the ‘environment’ tag

The – externally-managed-environment – in Python

without comments

To avoid this message which is to force you somehow to create a virtual environment in python:

$ pip install .
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.

Just remove this in Linux:

sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED

If you are using Homebrew just go to:

cd /home/linuxbrew/.linuxbrew/Cellar/python@3.12/3.12.2_1/lib/python3.12

and remove the file EXTERNALLY-MANAGED as well.

If on the other side, prefer to create a virtual environment, this is the way, install this package:

python3-venv

In the environment you prefer to work with, use this command:

python3 -m venv .venv

You can check all the installed packaged here:

user@server# ls .venv/
bin  include  lib  lib64  pyvenv.cfg

Then you have to activate the environment:

source .venv/bin/activate

Then it’s possible to use pip to install packages in that environment:

pip install -r requirements.txt
pip install openai

That’s it.

To deactivate the environment:

deactivate

Written by Felipe

March 6th, 2024 at 9:10 pm