Home >>Python Tutorial >The Python Modules

The Python Modules

The Python Modules

This is the python variables in modules tutorial. And as we have mentioned before in this tutorial, that a module can basically be defined as a type of file that contains a different set of functions that you can include in your overall application. To understand it in a better way you can consider any particular module to be similar to a type of code library.  

To Create a Module

If you wish to create a python module structure in this programming language then you can do that by simply saving the code that you desire by with a particular file extension of .py. For example, if you wish to save a particular code with a file name of mymodule.py then  
def greeting (name) :

print (“Hello, ” + name)
 

To Use a Module

If you wish to use the python module list that you just created then you can do that by using the statement of import. For example, if you wish to use the greeting function and if you further want to import the module that you earlier named mymodule then  
import mymodule

 

mymodule.greeting (“Harry”)
  It is also important for you to remember that whenever you use any particular function in a python module list then you should use make it a point to always use the syntax of module_name.function_name.  

The Variables in the Modules

As we have mentioned, modules can contain both functions and variables. The python variables in modules can include objects, dictionaries, arrays, and many other types of variables. For example, if you wish to save a particular code as a file named mymodule.py then  
person1 = {

“name” : “Sam”,

“age” : 36,

“country” : “Norway”

}
  If you further wish to import the module that we created above and then access the person1 dictionary then you can do that too by following the below-mentioned method.  
import mymodule

 

a = mymodule.person1 [“age”]

print ( a )
 

Name a Module

It is important for you to know that you can name the python module structure that you create anything that you like but you must always remember to use a particular kind of .py file extension whenever you save a module.  

To Rename a Module

You can use the as keyword in this programming language to create a particular kind of alias while importing any kind of module from the entire python module list. For example, if you wish to create an alias with a name of mx then  
import mymodule as mx

 

a = mx.person1 [“age”]

print ( a )
 

The Built-In Modules

There are also a number of different modules in this programming language that is built-in. You can also choose to import any of those built-in modules. For example, if you wish to import and use a type of platform module then  
import platform

 

x = platform.system ( )

print ( x )
 

To Use the dir ( ) Function

There is also a particular built-in function in this programming language that you can use to list all the names of the function or python variables in modules that are present in a particular module. For example, if you wish to use this function to list the entire particular defined names that belong to the module that we created above then  
import platform

 

x = dir ( platform )

print ( x )
  It is also important for you to know that you can use this particular function on any kind of module that you create.  

Import From Modules

If you wish to import some of the parts of a particular python module structure then you can do that by using the from keyword. For example, mymodule is a module that we created above which has a single dictionary and one function  
def greeting (name) :

print (“Hello, ” + name)

 

person1 = {

“name” : “John”,

“age” : 36,

“country” : “Norway”

}
  If you further only wish to import the person1 dictionary from the module that we created then you can do that too by following the below-mentioned method.  
from mymodule import person1

 

print ( person1 [“age”] )
  It is also important for you to remember that when you are using the from keyword to import anything then you should refer to the various elements of the module instead of using the module name. For example, it should be person1 [“age”] and it should not be mymodule.person1 [“age”]. With this, we finish the Python variable in modules part of our tutorial.

No Sidebar ads