Appearance
Installing Third-Party Modules
In Python, third-party modules are installed using the package management tool pip
.
If you are using Mac or Linux, you can skip the installation of pip
itself, as it is usually included with Python installations.
If you are using Windows, please refer to the section on installing Python to ensure that the options for installing pip
and "Add python.exe to Path" are checked.
To check if pip
is installed, try running it in the command prompt. If you receive a "command not found" error in Windows, you can re-run the installer to add pip
.
Note: On Mac or Linux, there may be both Python 3.x and Python 2.x installed, so the corresponding pip
command is pip3
.
For example, to install a third-party library called the Python Imaging Library (PIL), which is a powerful image processing tool for Python, you would typically use the Pillow project, which is a more actively maintained fork of PIL that supports the latest versions of Python 3.
Generally, third-party libraries are registered on the official Python Package Index (PyPI) at pypi.python.org. To install a third-party library, you first need to know the library's name, which can be found on the library's official site or on PyPI. For example, the name for Pillow is simply Pillow
, so the command to install it would be:
bash
pip install Pillow
After waiting patiently for the download and installation to complete, you can start using Pillow.
Installing Common Modules
While using Python, we often need various third-party libraries, such as Pillow, MySQL drivers, the Flask web framework, and the scientific computing library NumPy. Installing each library one by one with pip
can be time-consuming and may require checking for compatibility issues.
We recommend using Anaconda, which is a data processing and scientific computing platform based on Python. Anaconda comes pre-installed with many very useful third-party libraries, so installing it is like installing dozens of third-party modules automatically, making it very easy to use.
You can download the GUI installation package from the Anaconda website. The installer is around 500 to 600 MB, so it may take some time to download. After downloading, simply install it. Anaconda will redirect the python
command in the system PATH
to its own Python installation, and the third-party modules installed by Anaconda will be located in its own directory, ensuring that the system's installed Python directory remains unaffected.
After successfully installing Anaconda, reopen the command line window and type python
. You should see Anaconda's information:
┌────────────────────────────────────────────────────────┐
│Command Prompt - python - □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0] │
│(c) 2015 Microsoft Corporation. All rights reserved. │
│ │
│C:\> python │
│Python 3.6.3 |Anaconda, Inc.| ... on win32 │
│Type "help", ... for more information. │
│>>> import numpy │
│>>> _ │
│ │
└────────────────────────────────────────────────────────┘
You can now try directly importing installed third-party modules like numpy
.
Module Search Path
When we try to load a module, Python searches for the corresponding .py
file in the specified paths. If it cannot find the module, it will raise an error:
python
>>> import mymodule
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mymodule
By default, the Python interpreter searches in the current directory, all installed built-in modules, and third-party modules. The search paths are stored in the sys
module's path
variable:
python
>>> 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 search directory, there are two methods:
Modify
sys.path
directly: You can append your desired directory tosys.path
:python>>> import sys >>> sys.path.append('/Users/michael/my_py_scripts')
This method modifies the path at runtime but will not persist after the script finishes running.
Set the
PYTHONPATH
environment variable: This variable will be automatically added to the module search path. You can set it similarly to how you would set thePath
environment variable. Note that you only need to add your custom search path; the default Python search paths are unaffected.