A module name and custom modules

A module name and custom modules

In Python, a module is a file containing Python definitions, statements, and functions that can be imported and used in other Python code. A module name is the name of the file without the .py extension.

To use a custom module in Python, it must be stored in a location that is on the Python search path. The search path is a list of directories that the interpreter looks in when trying to import a module. The search path can be modified at runtime using the sys.path list.

To import a custom module, you can use the import statement followed by the module name. 

For example, suppose we have a module named my_module.py containing a function called my_function(). We can import and use this function in our main program like this:

java

import my_module

result = my_module.my_function()

Here, we use the import statement to import the my_module module and then call the my_function() function using dot notation.

Alternatively, you can use the from-import statement to import specific names from a module into the current namespace, as described in the previous question. Creating custom modules can be a useful way to organize and encapsulate code into reusable and maintainable parts. When creating a custom module, it’s a good idea to give it a descriptive name that reflects its purpose and to use best practices for module structure and documentation. This can make it easier for others to use and understand the module and can help avoid naming conflicts with other modules.

Apply for Python Certification!

https://www.vskills.in/certification/certified-python-developer

Back to Tutorials

Share this post
[social_warfare]
The from import statement
Virtual LAN

Get industry recognized certification – Contact us

keyboard_arrow_up