Getting Help From Python Interpreter

Whether we want some help for a module or want to quickly test a piece of code, Python interpreter is of great help. And in this article we are going to cover different ways where Python interpreter comes handy.

Introduction
 
Whether we want some help for a module or want to quickly test a piece of code, Python Interpreter is of great help. And in this article, we are going to cover different ways where Python Interpreter comes handy. In the previous articles, we have,
So, what are you waiting for? Start your interpreter and follow along as we talk about,
  • Getting help from the interpreter
  • Importing a module
  • Listing all the available modules
  • Executing some Python code
Let's get started!
 
help()
 
The very first helpful function that I want to talk about is the builtin help() function. It comes from the __builtins__ module and is of great help. There are two ways to use help — interactive help or help about an object.
 
Interactive Help
 
To start up, make sure that you have the interpreter ready. Now, type help() and press the return key. You should now see a screen similar to this one,
 
Python Interpreter 
 
The help> shows that our interactive help is ready to help us. Now we can use it to get help for different modules and functions. Let's give it a try, and search for math and math.pow as shown below,
  1. help> math  
  2.   
  3. Help on module math:  
  4.   
  5. NAME  
  6.     math  
  7.   
  8. MODULE REFERENCE  
  9.     https://docs.python.org/3.7/library/math      
  10.     The following documentation is automatically generated from the Python  
  11.     source files.  It may be incomplete, incorrect or include features that  
  12.     are considered implementation detail and may vary between Python  
  13.     implementations.  When in doubt, consult the module reference at the  
  14.     location listed above.  
  15.   
  16. DESCRIPTION  
  17.     This module is always available.  It provides access to the  
  18.     mathematical functions defined by the C standard.  
  19.   
  20. FUNCTIONS  
  21.     acos(x, /)  
  22.         Return the arc cosine (measured in radians) of x.  
  23.       
  24.     acosh(x, /)  
  25.         Return the inverse hyperbolic cosine of x.<br>  
  1. help> math.pow  
  2.   
  3. Help on built-in function pow in math:  
  4.   
  5. math.pow = pow(x, y, /)  
  6.     Return x**y (x to the power of y).  
  7. (END)  
We can exit the help document and the interactive help by pressing the 'q' key and then pressing return key.
 
Help about Object
 
We often want quick help on some module rather than entering the interactive help, and that's where help about object comes handy. So, when our interpreter is ready we can get help about a module or function by using the help(<object>) function. To get help for math.pow we first need to import the math module and ask for help on the pow function. Please refer to the code below,
  1. >>> import math  
  2. >>> help(math.pow)  
  3. Help on built-in function pow in math:  
  4.   
  5. math.pow = pow(x, y, /)  
  6.     Return x**y (x to the power of y).  
  7. (END)  
Please note that we have to import the math module first, otherwise we get an error as shown below,
  1. >>> help(math.pow)  
  2. Traceback (most recent call last):  
  3.   File "<stdin>", line 1in <module>  
  4. NameError: name 'math' is not defined  
Isn't it annoying to import a module to get help on it. But, there is another way to get help on a specific module without importing it. We can find all the available modules using the help("modules") function call. And it would return a result something like this,
 
Python Interpreter 
 
It is obvious that the list shown in the image above is not complete. Okay, now that we know about the available modules, we can now get help on them and this time we are not even going to import one.
 
Yes, we can get the details of a module without importing it, and to do so we use the help("module_name") function, as shown below:
  1. >>> help("flask")  
  2.   
  3. Help on package flask:  
  4.   
  5. NAME  
  6.     flask  
  7.   
  8. DESCRIPTION  
  9.     flask  
  10.     ~~~~~  
  11.       
  12.     A microframework based on Werkzeug.  It's extensively documented  
  13.     and follows best practice patterns.  
  14.       
  15.     :copyright: © 2010 by the Pallets team.  
  16.     :license: BSD, see LICENSE for more details.  
  17.   
  18. PACKAGE CONTENTS  
  19.     __main__  
  20.     _compat  
  21.     app  
  22. ...  
import
 
The import command helps you to import a module that is not currently available. By default, commonly used modules like math, random, web, datetime etc. are not available and ready to use. To use these modules or any other module we first need to import that module and then use it. You can do so by simply using the import <module> command, as we did earlier with our help function.
 
Think of a scenario where you have installed flask, however, while running your app you are getting ModuleNotFoundError. That's when the import command comes to the rescue. If the module import is successful, nothing is returned from the command. But if the import fails, we get an error as shown below,
  1. >>> import math   #successful import  
  2. >>> import flask  #import fails  
  3. Traceback (most recent call last):  
  4.   File "<stdin>", line 1in <module>  
  5. ModuleNotFoundError: No module named 'flask'  
dir()
 
Another helpful function that I want to talk about is the builtin dir() function. Similar to the dir command on Windows/DOS, the dir function, when called without an argument, returns you the names of different modules in the current scope.
  1. >>> dir()  
  2. ['__annotations__''__builtins__''__doc__''__loader__''__name__''__package__''__spec__']  
  3. >>>  
  4. >>>  
When used with an object, it returns an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it. The help(dir) defines it as,
"If the object supplies a method named __dir__, it will be used; otherwise the default dir() logic is used and returns,
  • for a module object: the module's attributes.
  • for a class object: its attributes, and recursively the attributes of its bases.
  • for any other object: its attributes, its class's attributes, and recursively the attributes of its class's base classes."
The image below shows the result of dir(__builtins__) function call,
 
Python Interpreter 
 
When we import any module, say math module, then we can see those modules in the response or result of the dir() function. Also, if we have some objects created in that scope then we will see those objects as well.
  1. >>> dir()  
  2. ['__annotations__''__builtins__''__doc__''__loader__''__name__''__package__''__spec__''math''numbers''os''random']  
  3. >>>  
  4. >>>  
Executing Code
 
Now that we do understand how to get help from the interpreter, it's time to see how we can use that help and execute some code for quick testing. So, let's create a list of integers and then print the square of each number using the math.pow function.
 
Let's start the Python interpreter and create a list of integers and assign it to the variable numbers,
  1. >>> numbers = [12345]  
  2. >>> type(numbers)  
  3. <class 'list'>  
  4. >>>   
The type(numbers) function return the type of the variable or object reference. Let's now import the math module, iterate over the list of numbers and print the square of each one, as shown in the code below,
  1. >>> import math  
  2. >>> for number in numbers:  
  3. ...     square = math.pow(number, 2)  
  4. ...     print(square)  
  5. ...   
  6. 1.0  
  7. 4.0  
  8. 9.0  
  9. 16.0  
  10. 25.0  
  11. >>>  
If we mess something up, the interpreter also gives us detailed errors as shown below,
  1. >>> import math  
  2. >>> for number in numbers:  
  3. ...     print(math.pow(number*2))  
  4. ...   
  5. Traceback (most recent call last):  
  6.   File "<stdin>", line 2in <module>  
  7. TypeError: pow() takes exactly 2 arguments (1 given)  
Summary
 
The article focuses on getting us familiar and comfortable with the Python interpreter, whether it's about getting some help or quickly testing a piece of code. Undoubtedly the interpreter is where most beginners start to learn Python, and the more we use it the more comfortable and efficient we get with it. In the next article, we will be talking about pip and some of its most useful commands.