The Invent with Python Blog

Writings from the author of Automate the Boring Stuff.

How to Run Pip From The Python Interactive Shell with pipfromrepl

Sun 20 November 2022    Al Sweigart

UPDATE March 22, 2023: I now recommend using the one-liner import pip; pip.main(['install', 'module_name_here']) module to install modules from the interactive shell rather than the instructions in this blog post. This code is easier to remember and works on Python versions 2.7 and 3.5+. The only downside is that it doesn't work on Python 3.4, in which case, use the instructions below. Here in 2023, however, the number of students working from 3.4 is near nonexistent so this is unlikely to be an issue.

If you are an instructor leading a Python programming workshop, getting third-party PyPI packages installed on students' machines with pip has several hurdles: navigating the command-line, operating system differences, distinguishing between pip and pip3, dealing with multiple versions of Python, misconfigured PATH environment variables, and virtual environments are all possible pitfalls.

You can avoid all of this and save time by using pipfromrepl, which allows you to install PyPI packages from the interactive shell. These same steps work no matter what the computer's Python setup is.

To install pipfromrepl, copy and paste the following into the interactive shell:

import subprocess, sys; subprocess.run([sys.executable, '-m', 'pip', 'install', 'pipfromrepl'])

This code runs the interactive shell's own Python interpreter to run the pip module to install pipfromrepl. The pipfromrepl module itself uses this same code to install other third-party packages, albeit with a simple set of functions.

Once pipfromrepl is installed, you can begin to use its functions: pipfromrepl.install(), pipfromrepl.list(), and pipfromrepl.uninstall().

For example:

>>> import pipfromrepl

>>> pipfromrepl.list()
Package     Version Editable project location
----------- ------- -------------------------
pip         22.3.1
pipfromrepl 0.1.0   C:\github\pipfromrepl
setuptools  65.5.1
wheel       0.37.1

>>> pipfromrepl.install('pymsgbox')
Collecting pymsgbox
  Using cached PyMsgBox-1.0.9-py3-none-any.whl
Installing collected packages: pymsgbox
Successfully installed pymsgbox-1.0.9

>>> pipfromrepl.list()
Package     Version Editable project location
----------- ------- -------------------------
pip         22.3.1
pipfromrepl 0.1.0   C:\github\pipfromrepl
PyMsgBox    1.0.9
setuptools  65.5.1
wheel       0.37.1

>>> pipfromrepl.uninstall('pymsgbox')
Found existing installation: PyMsgBox 1.0.9
Uninstalling PyMsgBox-1.0.9:
  Would remove:
    c:\users\al\.virtualenvs\pipfromrepl-fxbqt5ki\lib\site-packages\pymsgbox-1.0.9.dist-info\*
    c:\users\al\.virtualenvs\pipfromrepl-fxbqt5ki\lib\site-packages\pymsgbox\*
Proceed (Y/n)?   Successfully uninstalled PyMsgBox-1.0.9

There's also a pip() function that lets you specify the command-line arguments you'd pass to pip:

>>> import pipfromrepl

>>> pipfromrepl.pip('list')

>>> pipfromrepl.pip('install pymsgbox')

>>> pipfromrepl.pip('uninstall pymsgbox')

You must have pip installed to install third party modules. Pip comes with Python on Windows and macOS, but on Linux you may need to run sudo apt-get install python3-pip from a terminal window to install pip. This command requires the admin password to run.


Learn to program for free with my books for beginners:

Sign up for my "Automate the Boring Stuff with Python" online course with this discount link.

Email | Mastodon | Twitter | Twitch | YouTube | GitHub | Blog | Patreon | LinkedIn | Personal Site