Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run pip from the Python interactive shell? [duplicate]

Tags:

python

pip

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.)

like image 841
Al Sweigart Avatar asked Jan 30 '26 05:01

Al Sweigart


1 Answers

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.

like image 103
Al Sweigart Avatar answered Feb 01 '26 20:02

Al Sweigart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!