Python/Python 모듈탐구

[Python] - 모듈탐구 pydoc - Python의 백과사전

사실 백과사전이라 할 것까지는 없다. Python의 백과사전은 언제까지나 Github와 google일 것이다. pydoc은 Python document의 약자인데, Python에서 사용되는 Keyword, 내장 모듈 등의 설명서를 나타낸다. 크게 2가지 사용법을 알아보겠다.


사용법 - 1 Python 인터프리터로 해석 

필자는 Python개발도구로 IDLE을 보통 사용한다. IDLE에서 다음과 같은 코드를 입력하자

>>> import pydoc
>>> pydoc.help()

Welcome to Python 3.9's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.9/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> 

pydoc에는 내장함수가 help하나 밖에 없다. help를 실행한 순간 help전용 대화형 인터프리터가 나온다. 예시로 yield 키워드를 help로 찾아보겠다.

help> yield
The "yield" statement
*********************

   yield_stmt ::= yield_expression

A "yield" statement is semantically equivalent to a yield expression.
The yield statement can be used to omit the parentheses that would
otherwise be required in the equivalent yield expression statement.
For example, the yield statements

   yield <expr>
   yield from <expr>

are equivalent to the yield expression statements

   (yield <expr>)
   (yield from <expr>)

Yield expressions and statements are only used when defining a
*generator* function, and are only used in the body of the generator
function.  Using yield in a function definition is sufficient to cause
that definition to create a generator function instead of a normal
function.

For full details of "yield" semantics, refer to the Yield expressions
section.

help> 

yield에 대한 사용법이 나온다. 리눅스 계열의 man명령어와 비슷하다고 볼 수 있겠다.  키워드 말고도 모듈도 검색할 수 있다.

help> pydoc
Help on module pydoc:

NAME
    pydoc - Generate Python documentation in HTML or text for interactive use.

MODULE REFERENCE
    https://docs.python.org/3.9/library/pydoc
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    At the Python interactive prompt, calling help(thing) on a Python object
    documents the object, and calling help() starts up an interactive
    help session.
    
    Or, at the shell command line outside of Python:
    
    Run "pydoc <name>" to show documentation on something.  <name> may be
    the name of a function, module, package, or a dotted reference to a
    class or function within a module or module in a package.  If the
    argument contains a path segment delimiter (e.g. slash on Unix,
    backslash on Windows) it is treated as the path to a Python source file.
    
    Run "pydoc -k <keyword>" to search for a keyword in the synopsis lines
    of all available modules.
    
    Run "pydoc -n <hostname>" to start an HTTP server with the given
    hostname (default: localhost) on the local machine.
    
    Run "pydoc -p <port>" to start an HTTP server on the given port on the
    local machine.  Port number 0 can be used to get an arbitrary unused port.
    
    Run "pydoc -b" to start an HTTP server on an arbitrary unused port and
    open a Web browser to interactively browse documentation.  Combine with
    the -n and -p options to control the hostname and port used.
    
    Run "pydoc -w <name>" to write out the HTML documentation for a module
    to a file named "<name>.html".
    
    Module docs for core modules are assumed to be in
    
        https://docs.python.org/X.Y/library/
    
    This can be overridden by setting the PYTHONDOCS environment variable
    to a different URL or to a local directory containing the Library
    Reference Manual pages.

DATA
    __all__ = ['help']
    help = <pydoc.Helper instance>

DATE
    26 February 2001

AUTHOR
    Ka-Ping Yee <ping@lfw.org>

CREDITS
    Guido van Rossum, for an excellent programming language.
    Tommy Burnette, the original creator of manpy.
    Paul Prescod, for all his work on onlinehelp.
    Richard Chamberlain, for the first implementation of textdoc.

FILE

이런 식으로 말이다.

 

사용법 - 2 웹페이지를 사용

웹페이지를 호스팅 하여 pydoc을 활용할 수 있다. python 2.X에는 pydocgui라는 tkinter(python의 gui프로그래밍을 도와주는 모듈)로 제작된 별도의 모듈이 있었는데, 3.X 버전부터는 명령 프롬포트에 다음과 같은 명령으로 localhost에 웹페이지를 호스팅 한다.

C:\Users>python -m pydoc -p 12345

이렇게 하면 12345번 포트인 http://localhost:12345에 python documentation이 호스팅 된다. 내부는 이런 모습이다.


자! 이번 시간에는 python의 백과사전인 pydoc에 대해서 알아보았다. 물론 대부분의 경우 공식 API홈페이지나 Github의 형님들의 힘을 빌리겠지만, 네트워크가 안 되는 상황이나, 구글 신의 힘을 빌려도 잘 모르겠는 사용법은 같이 참고할 수 있으면 좋다. 

* 여담인데, pydoc에 문법에 맞추어 내가 추가한 모듈의 사용법을 추가할 수도 있다. 물론 pydoc은 로컬에 한정한다.