1. Use pip To Install Python Third Party Modules.
In Python, installing third-party modules is done through the package management tool pip. If you’re using a Mac or Linux, you can skip the steps of installing the pip itself. If you are using Windows, please ensure that
Add python.exe to Path checkbox are checked during installation. Try to run
pip under the command prompt window, and if the windows prompt does not find the command, rerun the installer to add
Note: it is possible to have both Python 3.x and Python 2.x on a Mac or Linux, so the corresponding pip command is pip3. For example, we will install a third-party Library, the Python Imaging Library, which is a very powerful tool library for image processing in Python. However, PIL currently only supports Python 2.7 and has not been updated for several years, so development of the pil-based Pillow project is very active and supports the latest Python 3.
In general, third-party libraries are registered on the official Python pypi.python.org website, and to install a third-party library, you must first know the name of the library, which can be searched on the official website or on pypi. For example, the name of the Pillow is called Pillow, so the command to install it is:
pip install Pillow. After patiently waiting to download and install, you can use the Pillow module.
2. Install Common Python Modules.
When working with Python, we often need to use a lot of third-party libraries. For example, the Pillow mentioned above, as well as the MySQL driver, the web framework Flask, the scientific calculation module Numpy, and so on.
Installing one by one with pip is time-consuming and laborious and requires consideration of compatibility. We recommend using Anaconda directly, which is a python-based data processing and scientific computing platform. It has many very useful third-party libraries built in.
You can download the GUI installation package from the official website of Anaconda. After downloading, Anaconda will point the python in the system Path to its own python, and the third-party modules installed by Anaconda will be installed in Anaconda’s own Path without affecting the installed python directory of the system. After installing Anaconda, reopen the command-line window and enter python to see the information about Anaconda. You can try to import third party modules installed such as numpy.
192:tmp richard$ python3 Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import numpy numpy>>> numpy.abs(-1) 1 >>>
3. Module Search Path.
When we try to load a module, Python will search for the corresponding .py file in the specified path, and if cannot find it, an error will be reported.
>>> 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, and the search path is stored in the
path variable of the
>>> import sys >>> sys.path ['', '/Users/richard/anaconda3/lib/python36.zip', '/Users/richard/anaconda3/lib/python3.6', '/Users/richard/anaconda3/lib/python3.6/lib-dynload', '/Users/richard/anaconda3/lib/python3.6/site-packages', '/Users/richard/anaconda3/lib/python3.6/site-packages/aeosa']
There are two ways to add our own search directory.
One is to modify sys.path directly to add the directory you want to search
>>> import sys >>> sys.path.append('/Users/richard/my_py_scripts')
This method modify the path at run time, but fail after the operation.
The second method is to set the environment variable PYTHONPATH, the variable value will be automatically added to the module search path. The setup is similar to setting the Path environment variable.