Last updated on March 28, 2019
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 previous articles we have:
- setup Python environment on Linux based systems
- setup Python environment on Windows
- run our first Python script
So, what are you waiting for? start your interpreter and follow along as we talk about:
- getting help from the interpreter
- import a module
- listing all the available modules
- executing some Python code
Let’s get started!
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 object.
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:
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.pow as shown below:
help> math Help on module math: NAME math MODULE REFERENCE https://docs.python.org/3.7/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(x, /) Return the arc cosine (measured in radians) of x. acosh(x, /) Return the inverse hyperbolic cosine of x.<br>
help> math.pow Help on built-in function pow in math: math.pow = pow(x, y, /) Return x**y (x to the power of y). (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
To get help for
math.pow we first need to import the math module and ask for help on the pow function. Please refer the code below:
>>> import math >>> help(math.pow) Help on built-in function pow in math: math.pow = pow(x, y, /) Return x**y (x to the power of y). (END)
Please note that we have to import the math module first, otherwise we get an error as shown below:
>>> help(math.pow) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'math' is not defined
There is one more way to get help on a specific module. We can find all the available modules using the
help("modules") function call. And it would return a result something like this:
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:
>>> help("flask") Help on package flask: NAME flask DESCRIPTION flask ~~~~~ A microframework based on Werkzeug. It's extensively documented and follows best practice patterns. :copyright: © 2010 by the Pallets team. :license: BSD, see LICENSE for more details. PACKAGE CONTENTS __main__ _compat app ...
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 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 where import command comes to 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:
>>> import math #successful import >>> import flask #import fails Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'flask'
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.
>>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] >>> >>>
When used with an object, it returns an alphabetised 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:
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.
>>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'math', 'numbers', 'os', 'random'] >>> >>>
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
Let’s start the Python interpreter and create a list of integers and assign it to the variable numbers:
>>> numbers = [1, 2, 3, 4, 5] >>> type(numbers) <class 'list'> >>>
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:
>>> import math >>> for number in numbers: ... square = math.pow(number, 2) ... print(square) ... 1.0 4.0 9.0 16.0 25.0 >>>
If we mess something up, the interpreter also gives us detailed errors as shown below:
>>> import math >>> for number in numbers: ... print(math.pow(number*2)) ... Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: pow() takes exactly 2 arguments (1 given)
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 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.