The Invent with Python Blog

Writings from the author of Automate the Boring Stuff.

Sun 20 November 2022

How to Run Pip From The Python Interactive Shell with pipfromrepl

Posted by Al Sweigart in python   

As an instructor, environment setup is a tough hurdle for students to clear so they can start learning to program. You could use an online Python IDE or set up Brython to run Python in your browser. Installing a complete IDE like PyCharm or Microsoft Visual Studio Code is also an option, but these IDEs have complicated-looking user interfaces that can be intimidating. Using IDLE (which comes with Python) or the command-line interactive shell can make the first few steps of programming less intmidating. However, running pip from the command-line has several problems itself. To solve this, I created the pipfromrepl module.

Experienced developers consider the command-line to be "easy" if they've forgotten how difficult it was when they first learned (or have never had to teach a beginner to code). These concepts include:

  • The slight differences between Windows, macOS, and Linux terminals.
  • The concept of the hierarchical file system and "current working directory."
  • That "directory" and "folder" are names for the same thing.
  • Absolute and relative file paths.
  • Using the cd and ls/dir commands.
  • The PATH environment variable (and modifying the .bashrc file or System/User environment variables on Windows.)
  • Dealing with multiple Python installations.

pipfromrepl is meant to assist students and instructors. It's probably a good idea to not rely on it in production environments.

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

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

Using subprocess and sys.executable is how pipfromrepl itself works. pipfromrepl works on Python 2.7 and Python 3.4+ (the versions of Python that come with the pip module.)

Once pipfromrepl is installed, you can begin to use its functions:

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

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.