Python Modules:

What is a Module ?

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference.

Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.

Example :

The Python code for a module named aname normally resides in a file named aname.py. Here's an example of a simple module, Hello.py :

  1. #Save this code in a file named, Hello.py
  2. def name_func( name ):
  3. print ("Hello, ", name,":)")
  4. return

The import Statement :

You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax :
import module1[, module2[,... moduleN]

Example :

  1. #Save this code in a file named, my_file.py
  2. #Import module Hello
  3. import Hello
  4. #Now you can call defined function that module as follows
  5. Hello.name_func("AyaN")
  6. input()

Output :

python modules examples

Variables in Module :

The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc) :

Example :

Save this code in a file named, about_us.py

  1. #Save this code in a file named, about_us.py
  2. person1 = {
  3. "name": "AyaN",
  4. "age": 24,
  5. "country": "IND"
  6. }
  7. person2 = {
  8. "name": "Apu",
  9. "age": 22,
  10. "country": "IND"
  11. }
  12. person3 = {
  13. "name": "Dev",
  14. "age": 21,
  15. "country": "IND"
  16. }
  17. person4 = {
  18. "name": "Symul",
  19. "age": 20,
  20. "country": "BD"
  21. }

Save this code in a file named, my_file.py and Import the module named about_us .

  1. #Save this code in a file named, my_file.py
  2. import about_us
  3. val1 = about_us.person1["name"]
  4. print("Person1 name : ",val1)
  5. val2 = about_us.person2["age"]
  6. print("\nPerson2 age : ",val2)
  7. val3 = about_us.person2["country"]
  8. print("Person2 country : ",val3)
  9. val4 = about_us.person4["name"]
  10. print("\nPerson4 name : ",val4)
  11. val5 = about_us.person4["age"]
  12. print("Person4 age : ",val5)
  13. val6 = about_us.person4["country"]
  14. print("Person4 country : ",val6)
  15. input()

Output :

python module variables access

Naming a Module :

You can name the module file whatever you like, but it must have the file extension .py (example : Hello.py, about_us.py etc.)

Re-naming a Module :

You can create an alias when you import a module, by using the as keyword. Example :

Example :

  1. #Create an alias for about_us called abts
  2. import about_us as abts
  3. val1 = abts.person3["name"]
  4. print("Person3 name : ",val1)
  5. val2 = abts.person3["age"]
  6. print("Person2 age : ",val2)
  7. input()

Output :

python modules remane

Built-in Modules :

There are several built-in modules in Python, which you can import whenever you like.

Example :

  1. import platform
  2. x = platform.system()
  3. y = platform.release()
  4. print("System Platfrom Name : ",x)
  5. print("System Release/OS Version : ",y)
  6. input()

Output :

built-in modules in python

Using the dir() Function :

There is a built-in function to list all the function names (or variable names) in a module. The dir() function.

Example :

  1. #List all the defined names belonging to the platform module
  2. import platform
  3. x = dir(platform)
  4. print(x)
  5. input()

Output :

python the dir() function

Note : The dir() function can be used on all modules, also the ones you create yourself.


Next Update

Loading...

Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels