pipx icon indicating copy to clipboard operation
pipx copied to clipboard

Add pip bin to support ansible pip module installation

Open xiaoyao9184 opened this issue 2 months ago • 3 comments

How would this feature be useful?

I'm using ansible to inject new dependencies for package in pipx, for example installing jmespath for pipx ansible-core

I've noticed that the venv created by pipx is different and uses shared for pip, so there is no pip command in bin path

shared bin path

ls -l /usr/local/py-utils/shared/bin/
total 48
-rw-r--r-- 1 root pipx 1903 Mar 30 09:08 activate
-rw-r--r-- 1 root pipx  852 Mar 30 09:08 activate.csh
-rw-r--r-- 1 root pipx 1992 Mar 30 09:08 activate.fish
-rw-r--r-- 1 root pipx 8834 Mar 30 09:08 Activate.ps1
-rwxrwxr-x 1 root pipx  252 Mar 30 09:08 easy_install
-rwxrwxr-x 1 root pipx  252 Mar 30 09:08 easy_install-3.9
-rwxrwxr-x 1 root pipx  242 Mar 30 09:08 pip
-rwxrwxr-x 1 root pipx  242 Mar 30 09:08 pip3
-rwxrwxr-x 1 root pipx  242 Mar 30 09:08 pip3.10
-rwxrwxr-x 1 root pipx  242 Mar 30 09:08 pip3.9
lrwxrwxrwx 1 root pipx    7 Mar 30 09:08 python -> python3
lrwxrwxrwx 1 root pipx   16 Mar 30 09:08 python3 -> /usr/bin/python3
lrwxrwxrwx 1 root pipx    7 Mar 30 09:08 python3.9 -> python3

env ansible-core bin path

ls -l /usr/local/py-utils/venvs/ansible-core/bin
total 80
-rw-r--r-- 1 root   pipx 1921 Mar 30 09:08 activate
-rw-r--r-- 1 root   pipx  870 Mar 30 09:08 activate.csh
-rw-r--r-- 1 root   pipx 2010 Mar 30 09:08 activate.fish
-rw-r--r-- 1 root   pipx 8834 Mar 30 09:08 Activate.ps1
-rwxrwxr-x 1 root   pipx  249 Mar 30 09:08 ansible
-rwxrwxr-x 1 root   pipx  269 Mar 30 09:09 ansible-community
-rwxrwxr-x 1 root   pipx  250 Mar 30 09:08 ansible-config
-rwxrwxr-x 1 root   pipx  279 Mar 30 09:08 ansible-connection
-rwxrwxr-x 1 root   pipx  251 Mar 30 09:08 ansible-console
-rwxrwxr-x 1 root   pipx  247 Mar 30 09:08 ansible-doc
-rwxrwxr-x 1 root   pipx  250 Mar 30 09:08 ansible-galaxy
-rwxrwxr-x 1 root   pipx  253 Mar 30 09:08 ansible-inventory
-rwxrwxr-x 1 root   pipx  252 Mar 30 09:08 ansible-playbook
-rwxrwxr-x 1 root   pipx  248 Mar 30 09:08 ansible-pull
-rwxrwxr-x 1 root   pipx 1733 Mar 30 09:08 ansible-test
-rwxrwxr-x 1 root   pipx  249 Mar 30 09:08 ansible-vault
-rwxr-xr-x 1 vscode pipx 1728 Apr  9 01:43 jp.py
drwxr-sr-x 2 vscode pipx 4096 Apr  9 01:43 __pycache__
lrwxrwxrwx 1 root   pipx    7 Mar 30 09:08 python -> python3
lrwxrwxrwx 1 root   pipx   16 Mar 30 09:08 python3 -> /usr/bin/python3
lrwxrwxrwx 1 root   pipx    7 Mar 30 09:08 python3.9 -> python3

Describe the solution you'd like

This is a problem for the ansible.builtin.pip module, as it can't use calls like 'python -m pip', but only the pip command, Maybe add a soft link to pip command for pipx venv to reach compatibility?

Here's an ansible example

    - name: case1 create venv and install jmespath
      ansible.builtin.pip:
        name: jmespath
        virtualenv: /usr/local/py-utils/venvs/test1
        virtualenv_command: /usr/local/py-utils/venvs/pipx/bin/python -m venv

    - name: case2 install jmespath with existing venv created by pipx
      ansible.builtin.pip:
        name: jmespath
        virtualenv: /usr/local/py-utils/venvs/ansible-core
        virtualenv_command: /usr/local/py-utils/venvs/pipx/bin/python -m venv
      ignore_errors: yes

    - name: ansible cant support python -m pip
      ansible.builtin.pip:
        name: jmespath
        executable: /usr/local/py-utils/venvs/ansible-core/bin/python -m pip
      ignore_errors: yes

output

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
ok: [localhost]

TASK [case1 create venv and install jmespath] ******************************************************************************************************************************************************************
ok: [localhost]

TASK [case2 install jmespath with existing venv created by pipx] ***********************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unable to find pip in the virtualenv, /usr/local/py-utils/venvs/ansible-core, under any of these names: pip3, pip. Make sure pip is present in the virtualenv."}
...ignoring

TASK [ansible cant support python -m pip] **********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "cmd": "'/usr/local/py-utils/venvs/ansible-core/bin/python -m pip' install jmespath", "msg": "[Errno 2] No such file or directory: b'/usr/local/py-utils/venvs/ansible-core/bin/python -m pip'", "rc": 2, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
...ignoring

case1 test1 success, case2 ansible-core fail, because test1 added the pip command.

$ . /usr/local/py-utils/venvs/test1/bin/activate
(test1) $ which pip
/usr/local/py-utils/venvs/test1/bin/pip

Describe alternatives you've considered

Currently I'm using a bash wrapper script that will emulate the python -m pip command, not sure if there are any other issues with that.

    - name: create pip wrapper script
      become: true
      ansible.builtin.copy:
        dest: /usr/local/py-utils/venvs/ansible-core/bin/pip
        owner: root
        group: pipx
        mode: u+rwx,g+rwx,o+rx
        content: |
          #!/bin/bash

          exec /usr/local/py-utils/venvs/ansible-core/bin/python -m pip $@

    - name: install jmespath with existing venv created by pipx
      ansible.builtin.pip:
        name: jmespath
        virtualenv: /usr/local/py-utils/venvs/ansible-core
        virtualenv_command: /usr/local/py-utils/venvs/pipx/bin/python -m venv

xiaoyao9184 avatar Apr 09 '24 02:04 xiaoyao9184

Maybe we should add a special case (say pipx inject ansible pip) for this.

uranusjr avatar Apr 09 '24 03:04 uranusjr

looks great

xiaoyao9184 avatar Apr 10 '24 11:04 xiaoyao9184

Just double confirm about the enhancement here, maybe we should add an option --with-pip for venv creation related command?

huxuan avatar Apr 23 '24 22:04 huxuan