Python Modules

Python Modules


What is a Module?

Consider a code library module to be same.

A file with a set of functions that your program wants to include.

You can also search for these topics, python modules list, python modules example programs, how to create python modules, python modules code, how to python command modules, execute the modules documentation using python, check the python error modules, how to install modules in python, what is a python modules?.

Create a Module

In order to construct a module, save the code you want in the extension file .py :

Example :- Save this code in a file named mymodule.py :

def greeting(name):
  print("Hello, " + name)
You can also search for these topics, python create a module dynamically, example for generate a python module, create a module python keyword, how to process the module, execute python create a module program.

Use a Module

The module we just generated can now be used by using the import statement :

Example :- Create a "demo.py" file and write the below code to import "mymodule.py" and use the greeting function the following module :

import mymodule
mymodule.greeting("Suresh")

Output :-

Hello, Suresh

Note : Use syntax: module_name.function_name when using a function from a module.

You can also search for these topics, python use a module create, python Use a Module to found error, how to python module to get version, how to reload the module in python, check the python range module, python return use a module, use a module random function using python, Example for use a python module.

Variables in Module

As already explained, but additionally variables of various types (arrays, dictionaries, objects, etc) can be present on the function :

Example 1 :- Save this code in the file mymodule.py :

person1 = {
  "name": "John",
  "age": 36,
  "country": "Norway"
}

Example 2 :- Create a "demo.py" file and write the below code to import the module called mymodule.py and access the dictionary person1 :

import mymodule
a = mymodule.person1["age"]
print(a)

Output :-

36

You can also search for these topics, python variable in module between two number, Variables in Module best practices using python, Variables in Module condition in python, python has no member variables in module, how to create variables in python module, python variable in module keywords, example for python variable in module meaning.

Naming a Module

You can name the module file, but the extension.py must be used.



You can also search for these topics, python naming a module access, Naming a Module best practices in python, example for python Naming a Module, python has no member with Naming a Module, Naming a Module process in python.

Re-naming a Module

If you import a module, you can create an alias by using the as keyword :

Example :- Create an alias for mymodule called mx :

import mymodule as mx
a = mx.person1["age"]
print(a)

Output :-

36

You can also search for these topics, python renaming a module example, Re-naming a Module with python file name, how to remove the Re-naming a Module in python, how to Re-naming a Module and replace with python, python change Re-naming a Module by position.

Built-in Modules

In the Python, you are able to import various built-in modules whenever you want.

Example :- Import and use the platform module :

import platform
x = platform.system()
print(x)

Output :-

Windows

You can also search for these topics, python built in modules example, use of graphics in Built-in Modules in python, how to use the python built-in module keywords, python Built-in Modules to location.

Using the dir() Function

The dir() function is used to lists all function names and/or variables names from a module.

Example :- List all the names defined by the module platform :

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']

Note : The dir() function can be used in all modules, even your own.



You can also search for these topics, python using the dir function arguments, example for python dir() function, how to get the dir() function using python, python keyword for function of dir(), Using the dir() Function and methods in python, python dir function question and answer.

Import from Keyword

By utilising a from Keyword, you can only import pieces (specific variables or functions) from a module.

Example 1 :- mymodule has a single function and a single dictionary :

def greeting(name):
  print("Hello, " + name)
person1 = {
  "name": "John",
  "age": 36,
  "country": "Norway"
}

Example 2 :- Import from the module only the person1 dictionary :

from mymodule import person1
print(person1["age"])

Output :-

36

Note : Do not mention the module name when importing elements in the module when using a from keyword.

Example is person1["age"] and not mymodule.person1["age"].

You can also search for these topics, python import from module in same packages, Import From Module in parent directory using python, python Import From Module from another file, how to Import the Module custom package in python, find the python module from module, python check the import the error module, import the module file and function using python, Example for python Import From Module.