pypa / pipx

Install and Run Python Applications in Isolated Environments
https://pipx.pypa.io
MIT License
10.64k stars 418 forks source link

[docs]: `sudo pipx` does not work when `pipx` is installed with `pip --user` #1560

Open Lordfirespeed opened 1 month ago

Lordfirespeed commented 1 month ago
$ sudo pipx
sudo: pipx: command not found

This is a documentation issue (in my opinion) - having tried a few ways, I couldn't find a convenient command to call a --user installation of pipx as root.

Therefore, the remedial steps I would take here are

Lordfirespeed commented 1 month ago

Here's what I did to install pipx system-wide on a generic linux machine.

I chose to create a user for it because I wanted to use pipenv to simplify the update process, but one could just as easily skip that whole bit and install pipx into the venv with pip with /opt/pipx-cli owned by root.

  1. sudo adduser pipx-cli --group --system --disabled-password --home /opt/pipx-cli --shell /bin/bash - create a new user pipx-cli to manage pipx-cli
  2. export GLOBIGNORE=".:.." to ignore the special names ., .. from Bash glob results
  3. sudo cp /etc/skel/.* /opt/pipx-cli - /etc/skel is not used for system users, we have to do this ourselves
  4. sudo chown -R pipx-cli:pipx-cli /opt/pipx-cli/ - files copied from /etc/skel are still owned by root
  5. sudo chmod o+rx /opt/pipx-cli to enable read and execute perms to 'others' on pipx-cli's homedir
  6. sudo -u pipx-cli -i - start a login shell as user pipx-cli
  7. python3 -m pip install --user pipenv - install pipenv for pipx-cli
  8. source ~/.profile to add newly created directory containing the pipenv executable to $PATH
  9. mkdir .venv && pipenv install pipx - create a new virtualenv, .venv, using pipenv which contains pipx
  10. nano pipx-cli - create a new script named pipx-cli. Provide the contents:

     #!/bin/bash
    
     # https://stackoverflow.com/a/77663806/11045433
     DIRNAME=$(dirname $( readlink -f "${BASH_SOURCE[0]:-"$(command -v -- "$0")"}" ))
    
     source $DIRNAME/.venv/bin/activate
     pipx $@

    Press ctrl+x, then y, then Enter to save the file.

  11. chmod a+x pipx-cli - for all users (a), add (+) the permission to execute (x) pipx-cli
  12. Press ctrl+d to end the session as pipx-cli
  13. sudo ln -s /opt/pipx-cli/pipx-cli /usr/local/bin/pipx - make a command, pipx, available system-wide that invokes the pipx-cli script