In Python, installing third-party modules is accomplished via the package management tool pip.
If you are using Mac or Linux, you can skip the step of installing pip itself.
If you are using Windows, please refer to the section on installing Python to ensure that you checked the boxes for pip and “Add python.exe to Path” during installation.
Try running pip in the Command Prompt window. If Windows prompts that the command is not found, you can re-run the installer to add pip.
Note: Python 3.x and Python 2.x may coexist on Mac or Linux systems, so the corresponding pip command is pip3.
For example, let’s install a third-party library — the Python Imaging Library (PIL), a powerful image processing library for Python. However, PIL currently only supports Python 2.7 and has not been updated for years. Therefore, the Pillow project, which is based on PIL, is under active development and supports the latest Python 3 versions.
Generally speaking, third-party libraries are registered on the official Python website pypi.python.org. To install a third-party library, you must first know its name, which can be searched on the official website or PyPI. For instance, Pillow is listed under the name “Pillow” on PyPI, so the command to install Pillow is:
pip install Pillow
After waiting patiently for the download and installation to complete, you can start using Pillow.
When working with Python, we often need to use many third-party libraries — for example, Pillow (mentioned above), MySQL drivers, the web framework Flask, and the scientific computing library NumPy. Installing them one by one with pip is time-consuming and laborious, and compatibility must also be considered. We recommend using Anaconda directly: it is a data processing and scientific computing platform based on Python that comes pre-installed with many useful third-party libraries. Installing Anaconda automatically sets up dozens of third-party modules for you, making it extremely simple and easy to use.
You can download the GUI installer from the official Anaconda website. The installer is 500~600MB in size, so please wait patiently for the download to finish. After downloading, install it directly: Anaconda will point the python executable in the system Path to its own bundled Python version. Additionally, third-party modules installed via Anaconda are stored in Anaconda’s own directory and will not affect the system’s existing Python installation directory.
After installing Anaconda, reopen the command line window and enter python — you will see Anaconda’s information:
┌─────────────────────────────────────────────────────────┐
│Windows PowerShell - □ x │
├─────────────────────────────────────────────────────────┤
│Windows PowerShell │
│Copyright (C) Microsoft Corporation. All rights reserved.│
│ │
│PS C:\Users\liaoxuefeng> python │
│Python 3.13.3 |Anaconda, Inc.| ... on win32 │
│Type "help", ... for more information. │
│>>> import numpy │
│>>> │
│ │
└─────────────────────────────────────────────────────────┘
You can try importing pre-installed third-party modules like numpy directly.
When we attempt to load a module, Python searches for the corresponding .py file in specified paths. If it cannot be found, an error will be raised:
>>> import mymodule
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mymodule
By default, the Python interpreter searches the current directory, all installed built-in modules, and third-party modules. The search paths are stored in the path variable of the sys module:
>>> import sys
>>> sys.path
['', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6', ..., '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages']
If you want to add your own custom search directory, there are two methods:
sys.path directly to add the target directory:python运行>>> import sys >>> sys.path.append('/Users/michael/my_py_scripts') This method modifies the path at runtime and the change will be lost after the program exits.PYTHONPATH: the contents of this environment variable are automatically added to the module search path. The setup method is similar to configuring the Path environment variable. Note that you only need to add your custom search paths — Python’s default search paths remain unaffected.