While in a Python venv I get -> error: externally-managed-environment when using sudo

Python venv sourcing is not working properly in Manjaro (?)

$cd /data
$which python             
/usr/bin/python

$sudo -u user python -m venv project
$cd project
$source bin/activate                                                                                                                                                            
(313)$ sudo -u user pip install -U 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.
(313)$ which pip
/data/project/bin/pip
(313)$which python
/data/project/bin/python

So my question is why is my …
sudo -u user pip install -U pip
… command not working as I am clearly in my venv?

sudo -u user ./bin/pip install -U pip
Does work by the way, but still it should work no?

It works fine for me. :slight_smile:

Why are you using sudo?

$ mkdir /tmp/venvtest
$ cd /tmp/venvtest
$ python -m venv venv
$ cd venv
$ source bin/activate
$ pip install -U pip
Requirement already satisfied: pip in ./lib/python3.13/site-packages (24.3.1)
Collecting pip
  Using cached pip-25.0.1-py3-none-any.whl.metadata (3.7 kB)
Using cached pip-25.0.1-py3-none-any.whl (1.8 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 24.3.1
    Uninstalling pip-24.3.1:
      Successfully uninstalled pip-24.3.1
Successfully installed pip-25.0.1
1 Like

hmmm… what you did works for me too! Strange.

Is it the sudo command?

I am using sudo to start the command as the project user

Seems to be, I just checked and of course got the same result as you.

It’s not quite the same as being logged in, if you really need a different user then you could try su $USER or su $USER -, obviously replace $USER with the username. :man_shrugging:

Is the project user different from your user? Normally it’s run as your user.

1 Like

Never use sudo with pip - even within a venv sudo pip will install system wide and that is why you get the message.

Within venv calling pip (no sudo) will do what you ask in the confinement of the venv.

4 Likes

Why are you using sudo, in the first place?

Here’s how I set up a clean python project.

in my ~/Projects directory using a bash Terminal

mkdir someproject
cd someprject
python3 -m venv venv
source venv/bin/activate

once activated…

python3 -m pip install --upgrade pip
python3 -m pip install whatever-other-modules-needed
2 Likes

Yes it is. Like pgadmin or mayan or any other project I want then to run under their own username as they run in a semi-production environment (e.g. my own server).
I am sure venv is ‘normaly’ not run as your own user, only in development environments I guess.

But thanks, I understand now that sudo isn’t working in venv the way I used too (as it was before).
I will play around with su $USER see if that suits me. Still… sudo -u $USER should work in my opinion and it doesn’t.

I don’t see your perceived problem.

I’ve written websites in Python + CherryPy, using the same exact setup, as I described. Moved them to a Local web server for testing, then to an off site server for production.

What’s your perceived issue?

I certainly can’t develop the image conversion utilities, I posted to GitLab recently, under the user names of the people who might find them useful.

So what problem are you trying to solve, in this problematic manner?

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.