Stack Exchange
No module named 'pip' when "Getting requirements to build wheel" but pip does exist
In a new Docker container (based on Rocky 8) I built Python from source:
RUN curl https://www.python.org/ftp/python/3.11.4/Python-3.11.4.tgz --output Python-3.11.4.tgz && \
    tar -xzf Python-3.11.4.tgz && \
    cd Python-3.11.4 && \
    ./configure --enable-optimizations && \
    make -j4 && \
    make install && \
    cd .. && \
    rm -rf Python-3.11.4

After which I can run Python apparently fine:
[root@a0aa4fc36e39 opt]# python3 --version
Python 3.11.4
[root@a0aa4fc36e39 opt]# pip3 --version
pip 23.1.2 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)
[root@a0aa4fc36e39 opt]# python3 -m pip --version
pip 23.1.2 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)
[root@a0aa4fc36e39 opt]# python3
Python 3.11.4 (main, Jun  9 2023, 14:56:33) [GCC 8.5.0 20210514 (Red Hat 8.5.0-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pip
>>> 

However when I try to pip3 install path/to/riscof it fails with the apparently near-universal error message No module named 'pip'. (Here's riscof btw.).
Using pip 23.1.2 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)
Processing ./tools/riscof
  Running command pip subprocess to install build dependencies
  Collecting setuptools>=40.8.0
    Using cached setuptools-67.8.0-py3-none-any.whl (1.1 MB)
  Collecting wheel
    Using cached wheel-0.40.0-py3-none-any.whl (64 kB)
  Installing collected packages: wheel, setuptools
  Successfully installed setuptools-67.8.0 wheel-0.40.0
  WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
  Installing build dependencies ... done
  Running command Getting requirements to build wheel
  Traceback (most recent call last):
    File "/usr/local/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
      main()
    File "/usr/local/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
      json_out['return_val'] = hook(**hook_input['kwargs'])
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
      return hook(config_settings)
             ^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-trnsio_e/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 341, in get_requires_for_build_wheel
      return self._get_build_requires(config_settings, requirements=['wheel'])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-trnsio_e/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 323, in _get_build_requires
      self.run_setup()
    File "/tmp/pip-build-env-trnsio_e/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 488, in run_setup
      self).run_setup(setup_script=setup_script)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-trnsio_e/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 338, in run_setup
      exec(code, locals())
    File "<string>", line 3, in <module>
  ModuleNotFoundError: No module named 'pip'
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> See above for output.
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  full command: /usr/local/bin/python3.11 /usr/local/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py get_requires_for_build_wheel /tmp/tmprn976u9s
  Getting requirements to build wheel ... error
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

I'm not in a venv. I definitely have pip available. There's only one Python installation on the whole system. How exactly has Python screwed this up?

What happens if you try python -m pip install /path/to/package?

dw I figured it out

Ok it turns out this is because Riscof didn't list wheel in its dependencies and this is the kind of quality error message you get from Python when things don't go exactly as it expects.
pip3 install wheel solved it.

That's only for ancient packages that don't declare a PEP 517 interface. It's best if they fix this upstream, and also publish wheels.

Who can keep up with Python's package management mess? I feel like more of the blame lies with Python for not sorting this out a decade ago and for the absolutely abysmal error message.

I mean… There's a lot of good work going on in that direction. Anybody can assign blame, but it's more valuable to actually do something about it and get involved, isn't it? You're complaining about other people's free labor — feel free to ask for a refund

Ask that the individual who forced you.

My boss? I'm sure that will go well. It's not like he had an entirely free choice either. If you put something out in the world you don't get to shirk all responsibility for it just because you aren't charging money for it. Obviously.

And that's your excuse for acting like this? Jeez.

Acting like what? All I did was criticise the low quality of Python error messages. The error message here is objectively awful.

Attacking the packaging community that contributes a lot in their free time.

You should author a pyproject.toml that looks like this:
[build-system]
requires = ["setuptools", "wheel", "pip"]
build-backend = "setuptools.build_meta"

This is now required for pip install -e . where setup.py uses e.g. pip.

No, you mustn't add pip or wheel there. wheel is auto-added by setuptools. And pip is a front-end, not backend..

Pip shouldn't be used in setup.py FWIW.
