Installing Python modules with the pip tool is surprisingly hard to describe to beginners learning to code. There are several potential issues: multiple Python installations, virtual environments, PATH environment variable settings. You have to introduce command-line terminals and file system navigation, and the differences between Windows and Mac/Linux. However, there is a line of code you can run from the interactive shell to handle all this for you.
To install a Python module, run the following code from the interactive shell (that is, the thing with the >>> prompt):
import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', '--user', 'MODULE_NAME'])
If you're running the interactive shell from a virtual environment and you get an error like
ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv., then remove the
import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', 'MODULE_NAME'])
It's generally not advisable to run code that some website tells you to without understanding what it does, so let's break down each section.
import sys, subprocess;
This imports the
subprocess built-in modules so that we can access the
sys.executable variable and
subprocess.run() function. These modules are in Python's standard library, so they are always already installed with Python.
The semicolon makes Python consider it the "end of the line" so that we can fit two Python instructions on the same line.
This function call simulates running a command from the Terminal or Command Prompt window, except it's now done from Python code.
sys.executable variable contains the file path of the Python interpreter that is currently running the interactive shell. This is especially useful if you have multiple Python installations or virtual environments and want to run the current Python interpreter you are using.
'-m', 'pip', 'install', 'MODULE_NAME'])
When you run the Python interpreter with the
-m option, you are running a module (in our case, the standard
pip module) as a script. Running
python -m pip is the same thing as running the
pip tool itself. The
'install', 'MODULE_NAME' options tells pip to download and install a module from the online Python Packaging Index. Your computer needs to be connected to the internet when you run these instructions.
After running these instructions, you should see the output from pip in the window. For example, when I installed the pyrect module the interactive shell looked like this:
>>> import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', 'pyrect']) Collecting pyrect Using cached PyRect-0.2.0.tar.gz (17 kB) Preparing metadata (setup.py) ... done Building wheels for collected packages: pyrect Building wheel for pyrect (setup.py) ... done Created wheel for pyrect: filename=PyRect-0.2.0-py2.py3-none-any.whl size=11196 sha256=5b7fb5f14167a70c955fff3bb14eecfa557f07747f27a03b5c63834173a58f49 Stored in directory: c:\users\al\appdata\local\pip\cache\wheels\25\80\fa\27bb4a1c2e21f64ec71390489d52e57b7cc8afbe79bd595c5e Successfully built pyrect Installing collected packages: pyrect Successfully installed pyrect-0.2.0 CompletedProcess(args=['C:\\Users\\Al\\.virtualenvs\\deleteme-NM1HlCaN\\Scripts\\python.exe', '-m', 'pip', 'install', 'pyrect'], returncode=0)
To verify that the module installed correctly, run
import MODULE_NAME. If no error message appears, the module has been successfully installed.
If you're teaching a Python class or running a Python workshop, using this Python code is a great way to avoid confusion when you need them to install a third-party module from PyPI. If you want to go into more detail about the command-line, environment variables, and similar topics, you can read Chapter 2 of my free book, Beyond the Basic Stuff with Python.