库和扩展 FAQ

General Library Questions

How do I find a module or application to perform task X?

校验 the Library Reference to see if there’s a relevant standard library module. (Eventually you’ll learn what’s in the standard library and will be able to skip this step.)

For third-party packages, search the PyPI (Python 包索引) or try Google or another web search engine. Searching for “Python” plus a keyword or two for your topic of interest will usually find something helpful.

Where is the math.py (socket.py, regex.py, etc.) source file?

If you can’t find a source file for a module it may be a built-in or dynamically loaded module implemented in C, C++ or other compiled language. In this case you may not have the source file or it may be something like mathmodule.c , somewhere in a C source directory (not on the Python Path).

There are (at least) three kinds of modules in Python:

  1. modules written in Python (.py);

  2. modules written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc);

  3. modules written in C and linked with the interpreter; to get a list of these, type:

    import sys
    print(sys.builtin_module_names)