You can learn about Python Variable Scope Programs with Outputs helped you to understand the language better.
Python Programming – Import Model
A package is just a directory tree with some Python files in it. To use any package in your code, you must first make it accessible. You have to import it. In Python, modules are accessed by using the import statement. When you do this, you execute the code of the module, keeping the scopes of the definitions so that your current file(s) can make use of these. Some modules are available through the Python Standard Library which contains many modules that provide access to system functionality or provide standardized solutions and are therefore installed with your Python installation. Others can be installed with Python’s package manager pip. Additionally, you can create your own Python modules since modules are comprised of Python .py files.
There are a number of modules that are built into the Python Standard Library. Some of the commonly used built-in modules are os, sys, math, random, data time, etc. Import statement is used to get a module as a whole. When you use the from statement, you copy names from the module to the importer.
Writing and Importing Modules
A module is a file that contains a collection of related functions and other definitions. Writing a module is just like writing any other Python file. Modules can contain definitions of functions, classes, and variables that can then be utilized in other Python programs. You can make a module available to another program by importing it. For example, Python has a math module that includes a number of specialized mathematical tools, and it also defines some names, including pi. Here’s how you import:
>>> import math
Here’s how you access the name pi that it defines:
Note that you can import modules with the import command. To use a module, you can import it. Usually, import statements occur at the beginning of the Python module. Importing modules is a fairly simple operation, but it requires a little explanation. Consider the following examples:
- import math
- import os, sys
- from math import sqrt
- import os as operatingSystem
These examples highlight some variations in how you can import modules:
1. This first example is the simplest and easiest to understand. It is merely the keyword import fol-lowed by the module name (in this case, math). The standard way to import a module, say math, is to write:
and then access individual functions in the module with the module name and prefix as given below:
x = math.sqrt(y)
2. Multiple modules can be imported with the same import command, with the modules separated by a comma.
3. You can import specific names only within a module, without importing the whole module, by using the form <module> import <name> statement. This can be useful for performance reasons if you only need one function from a large module. Now you can work with sqrt directly, without the math prefix. More than one function can be imported:
from math import sqrt, exp, log, sin Sometimes one just writes: from math import *
to import all functions in the math module. This includes sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, exp, log (base e), loglO (base 10), sqrt, as well as the famous numbers e and pi. Importing all functions from a module, using the asterisk (*) syntax, is convenient, but this may result in a lot of extra names in the program that is not used.
It is, in general, recommended not to import more functions than those that are really used in the program
4. If a module has a name that is difficult to work with or remember, and you want to use a name to represent it that is meaningful to you, simply use the as keyword and import <module-name> as <specific-name>.
Let’s look at few examples to use import:
Create a Python module area.py as shown in Figure 12.16. This module import the “math” module. It contains multiple mathematical functions. Among them is the pi() function, which provides the value of pi which is used to calculate area of circle.
Demo of import.
|# To create area module area.py
return length * breadth
Demo of Importing module area.py in mathl.py module.
Now let us use the area.py module in another module mathl.py to calculate area_circle, area_square, and area_rectangle.
|# Importing module area.py in mathl.py module
You can use dir( ) function which list all objects such >>> import area as sub-modules, variables, constants defined in the >>> dir (area)
module. To list all the objects present in area.py, It will display all objects as shown in Figure 12.18. type:
|>>> import area
[ __builtins_’, ,_cached_,) ‘_doc_’, * file ,_Joader_J, ,__name_\ ‘_package_ ; ‘_spec_\ ’area_circle’, ‘area_rectangle’, ’area.square’, ‘math ]
Demo of from-import statement.
With the from-import statement, you can select specific functions from the module. For example, to calculate area of circle only, that call specific sub-module from-import statement is used as – shown in Figure 12.19.
|# To call specific function module in math2.py module
from area import area_circle
Demo of import * statement.
We can also import all the attributes from a module by using *. For example, you can import all the sub-module from module “area.py” as shown in Figure 12.20.
|# To import all the sub-modules from module “area.py” in “math3.py”
from area import *
Python Module Search Path
Whenever you import Python modules, say hello, the interpreter searches a built-in version. If not found, it searches for a file named hello.py under a 3. list of directories given by variable sys.path. The sys.path is initialized from the environment variable PYTHONPATH. PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load. The environment variable PYTHONPATH is empty by default.
- PYTHONPATH is an environment variable that is used when a module is imported.