Modules in Python - Letsprogram

Share:
We use functions to decrease the number of lines in the program and of course reusability. But any functions can be used in the same program but not outside. If you quit your interpreter the definitions you made are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script.



Generally, developers handling with larger programs will split into multiple files for easy use. And also to use a function in another file instead of copying several times into each program we use scripting.

Python support this it has a way to put functions or definitions in an interactive instance of an interpreter called a module. We all know we import some modules into our programs by importing that module means we are importing some definitions which make our work fast and easier. Python has many modules.

The module is just a normal python file like any other with ends with .py extension. modules name is available for the value of global variable __name__.

>>>import pandas as p
>>>p.__name__
'pandas'

Create a file named prime.py in the current directory you are working and enter the following codes.


Now enter the Python interpreter and import this module with the following command:

>>>import prime.py

Now you had imported the definitions written in the prime.py file into your program without using the function names.

>>>prime.PRIME(3)
'prime'
>>>prime.evenodd(2)
'even'
prime.__name__
'prime'

If you use any function many times in your program you can change the name by

>>>even = prime.evenodd
>>>even(3)
'odd'

A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement. (They are also run if the file is executed as a script.)

Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module. Thus, the author of a module can use global variables in the module without worrying about accidental clashes with a user’s global variables. On the other hand, if you know what you are doing you can touch a module’s global variables with the same notation used to refer to its functions, modname.itemname.

Modules can import other modules. It is customary but not required to place all import statements at the beginning of a module (or script, for that matter). The imported module names are placed in the importing module’s global symbol table.

There is an another way to import all the definitions from the module.

>>>from pandas import *

This imports all the names except those starting with _. In most cases, programmers don't use those functions.

Note that in general the practice of importing * from a module or package is frowned upon since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.

Sometimes we do not require every definition in the module if we imported very large modules which means your program has many statements to run which may decrease your program efficiency. so, to avoid that there is another way to import a  module.

For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter – or, if it’s just one module you want to test interactively, use importlib.reload(), e.g. import importlib; importlib.reload(modulename).


>>>from pandas import Series

>>>from pandas import DataFrame as df


When a module prime is imported then python interpreter searches in the built-in modules first then it checks a file name prime.py in a list of directories given by sys.path. The directories like current, default, or directory names with the same syntax as shell variable path.

To load a module speedily python caches the module in __pycache__ directory under the named module.version.pyc where the version indicates the format of the compiled file. For Example, base_embed.cpython-38.pyc  is the compiled file to load the modules fastly. __pycache__ is available in the              \Python38Lib\site-packages\ in this place.

Python checks the modification date of the source against the compiled version to see if it’s out of date and needs to be recompiled. This is a completely automatic process. Also, the compiled modules are platform-independent, so the same library can be shared among systems with different architectures.

Python does not check the cache in two circumstances. First, it always recompiles and does not store the result for the module that’s loaded directly from the command line. Second, it does not check the cache if there is no source module. To support a non-source (compiled only) distribution, the compiled module must be in the source directory, and there must not be a source module.

We discussed some of the standard modules in previous posts. The virtual environment is also discussed in the previous posts

To use a module we need to know the functions or statements in that module. To know that use dir( ) built-in function which gives you the name of all the strings. import the module into your interpreter and use dir(modulename) to get all the methods. This is not only for modules and you can know about any object by using this function.

Packages are the way of structuring the Python's module namespace by using the dotted module names.

Here is a possible structure for a package.


No comments

F