Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Interpreter Mode - What are some ways to explore Python's modules and its usage

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!

like image 842
Jack Chi Avatar asked Feb 06 '26 21:02

Jack Chi


1 Answers

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.
like image 84
khachik Avatar answered Feb 08 '26 10:02

khachik