From the interactive shell (which has the >>> prompt) run the following, replacing MODULE_NAME with the name of the module you want to install:
import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', '--user', 'MODULE_NAME'])
You'll see the output from pip. You can verify that the module installed successfully by trying to import it:
import MODULE_NAME
If no error shows up when you import it, the module was successfully installed.
I want to install third-party Python modules from the Python interactive shell rather than the Terminal or Command Prompt. What Python instructions do I run to do this so I can then import the installed module and begin using it?
(For those asking why I want to do this: I need reliable instructions for installing third-party modules for users who don't know how to navigate the command-line, don't know how to set their PATH environment variable, may or may not have multiple versions of Python installed, and can also be on Windows, Mac, or Linux and therefore the instructions would be completely different. This is a unique setup, and "just use the terminal window" isn't a viable option in this particular case.)
How do I run pip from the Python interactive shell?