Python Interpreter Mode - What are some ways to ex

2019-05-13 23:34发布

问题:

While inside the Python Interpreter:

What are some ways to learn about the packages I have?

>>> man sys
File "<stdin>", line 1
    man sys
      ^

SyntaxError: invalid syntax

>>> sys --help
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary -: '_Helper'

Corrected:

>>> help(sys)
...

Now, how do I see all the packages available to me on my sys.path? And see their subsequent usage and documentation. I know that I can easily download a PDF but all this stuff is already baked in, I'd like to not duplicate files.

Thanks!

回答1:

You can look at help("modules"), it displays the list of available modules. To explore a particular module/class/function use dir and __doc__:

>>> import sys
>>> sys.__doc__
"This module ..."

>>> dir(sys)
[..., 'setprofile', ...]

>>> print(sys.setprofile.__doc__)
setprofile(function)

Set the profiling function.  It will be called on each function call
and return.  See the profiler chapter in the library manual.


回答2:

Python has an extensive built-in help system which you can access with help():

>>> help()

Welcome to Python 2.7!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/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, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help> modules

Please wait a moment while I gather a list of all available modules...

BaseHTTPServer      base64              importlib           sha
Bastion             bdb                 imputil             shelve
CGIHTTPServer       binascii            inspect             shlex
Canvas              binhex              io                  shutil
[...]

Enter any module name to get more help.  Or, type "modules spam" to search
for modules whose descriptions contain the word "spam".

help> base64
Help on module base64:

NAME
    base64 - RFC 3548: Base16, Base32, Base64 Data Encodings

FILE
    /usr/local/lib/python2.7/base64.py

MODULE DOCS
    http://docs.python.org/library/base64

FUNCTIONS
    b16decode(s, casefold=False)
        Decode a Base16 encoded string.

        s is the string to decode.  Optional casefold is a flag specifying whether
        a lowercase alphabet is acceptable as input.  For security purposes, the
        default is False.

        The decoded string is returned.  A TypeError is raised if s were
        incorrectly padded or if there are non-alphabet characters present in the
        string.

    b16encode(s)
        Encode a string using Base16.

        s is the string to encode.  The encoded string is returned.

    b32decode(s, casefold=False, map01=None)
        Decode a Base32 encoded string.

        s is the string to decode.  Optional casefold is a flag specifying whether
        a lowercase alphabet is acceptable as input.  For security purposes, the
        default is False.

        RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O
        (oh), and for optional mapping of the digit 1 (one) to either the letter I
        (eye) or letter L (el).  The optional argument map01 when not None,
        specifies which letter the digit 1 should be mapped to (when map01 is not
        None, the digit 0 is always mapped to the letter O).  For security
        purposes the default is None, so that 0 and 1 are not allowed in the
        input.

        The decoded string is returned.  A TypeError is raised if s were
        incorrectly padded or if there are non-alphabet characters present in the
        string.

    b32encode(s)
        Encode a string using Base32.

        s is the string to encode.  The encoded string is returned.


回答3:

There is a search engine called nullege to find out how a particular module or object is used in source code.

See the example for os

I wrote a python interface for that at github