Python Tutorial

Python Modules- How to Create, Use and Variables

Table of Contents

  • What are Modules in Python Programming?
  • How to Create a Module in Python?
  • How to Use Python Module?
  • What are Module Variables in Python?
  • Naming a Module
  • Renaming a Module
  • Built-in Modules in Python
  • Using the dir() Function
  • Importing From Module

What are Modules in Python Programming?

Modules in Python are simply ".py" files that contain Python code that may be imported into another Python program.

A module may be thought of as a code library or a file containing a group of functions that you want to add to your program.

We may use modules to group together similar functions, classes, or code blocks in the same file. As a result, splitting large Python code blocks into modules comprising up to 300–400 lines of code is regarded as the best practices for building larger Python code blocks for production-level projects in Data Science.

How to Create a Module in Python?

Write the code you want in a file with the extension.py to build a module:

Example:

def greeting(name):

  print("Hello, " + name)

How to Use Python Module?

Using the import line, we can now use the module we just created:

Example:

Import the module named my_module, and call the greeting function:

import my_module

my_module.greeting("Wscube")

Output:

C:\Users\My Name>python demo1.py
Hello, Wscube

What are Module Variables in Python?

The module can include not only functions, but also variables of any type (arrays, dictionaries, objects, and so on):

Example:

Save this code in the file my_module.py

p1 = {

  "name": "John",

  "age": 23,

  "country": "india"

}

Example:

import my_module

a = my_module.p1["age"]

print(a)

Output:

C:\Users\My Name>python demo2.py
23

Naming a Module

The module file can be named whatever you like, but it must have the.py extension.

Renaming a Module

If you wish to import a module, you can use the as keyword to create an alias:

Example:

Create an alias for mymodule called mx:

import mymodule as mx

a = mx.person1["age"]

print(a)

Built-in Modules in Python

Python comes with a number of built-in modules that you can use whenever you want.

C:\Users\My Name>python demo_module3.py

Output:

36

Example:

Import and use the platform module:

import platform

x = platform.system()

print(x)

Output:

Windows

Using the dir() Function

To list all the function names in a module, there is a built-in function. The dir() method is used to:

Example:

import platform

x = dir(platform)

print(x)

Output:

['DEV_NULL', '_UNIXCONFDIR', 'WIN32_CLIENT_RELEASES', 'WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', '__loader__', '__name__', '__package __', '__spec__', '__version__', '_default_architecture', '_dist_try_harder', '_follow_symlinks', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser', '_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version', '_mac_ver_xml', '_node', '_norm_version', '_perse_release_file', '_platform', '_platform_cache', '_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists', '_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver', '_uname_cache', '_ver_output', 'architecture', 'collections', 'dist', 'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation', 'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys', 'system', 'system_aliases', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']

Importing From Module

Using the from keyword, you can choose to import only pieces from a module.

Example:

There is only one function and one dictionary in the module named mymodule:

def greeting(name):

  

print("Hello, " + name)

person1 = {

  "name": "John",

  "age": 36,

  "country": "Norway" }
Did you find this article helpful?