Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this Python Modules tutorial, we'll cover the basics of using modules in Python, including importing and organizing code into modules.
__name__
attributeA module in Python is simply a file containing Python code. Modules are used to organize code into separate, reusable components, which can then be imported and used in other programs. By splitting code into modules, you can reuse functionality across multiple projects and keep your codebase organized and maintainable.
To use a module in your Python code, you need to import it using the import
statement. When you import a module, Python will search for the module in its module search path, which includes the current directory and the directories listed in the sys.path
variable.
Here's an example of importing a built-in module, the math
module:
import math # Now you can access the functions and variables in the math module result = math.sqrt(25) print(result) # Output: 5.0
You can also import specific functions or variables from a module using the from ... import ...
statement:
from math import sqrt result = sqrt(25) print(result) # Output: 5.0
To import multiple items from a module, separate them with commas:
from math import sqrt, pi result = sqrt(pi) print(result) # Output: 1.77245385091
To avoid naming conflicts or make your code more readable, you can use the as
keyword to create an alias for the imported module, function, or variable:
import math as m result = m.sqrt(25) print(result) # Output: 5.0
To create your own module, simply create a new .py
file and define your functions, classes, and variables within it. Let's create a simple module called mymodule.py
:
# mymodule.py def greet(name): return f"Hello, {name}!"
Now you can import and use your custom module in other Python programs:
import mymodule greeting = mymodule.greet("Alice") print(greeting) # Output: Hello, Alice!
__name__
attributeEvery Python module has a built-in attribute called __name__
. When a module is run as the main program, its __name__
attribute is set to "__main__"
. When a module is imported, its __name__
attribute is set to the module's name.
You can use the __name__
attribute to conditionally execute code in your module. For example, you might have some code that you want to run only when the module is executed as the main program, but not when it's imported:
# mymodule.py def greet(name): return f"Hello, {name}!" def main(): print(greet("Alice")) if __name__ == "__main__": main()
Now, when you run mymodule.py
as the main program, it will execute the main()
function. But when you import mymodule
in another Python program, the main()
function will not be executed.
How to create a module in Python:
.py
extension.# mymodule.py def greeting(name): return f"Hello, {name}!"
Importing modules in Python:
import
keyword, allowing access to the functions and variables defined in the module.import mymodule message = mymodule.greeting("Alice") print(message)
Built-in modules in Python:
math
, random
, and datetime
.import math square_root = math.sqrt(25) print(square_root)
Creating and using custom modules in Python:
# mycustommodule.py def multiply(x, y): return x * y # main script import mycustommodule result = mycustommodule.multiply(3, 4) print(result)
Exploring standard library modules in Python:
os
, sys
, and collections
for different functionalities.import os current_directory = os.getcwd() print(current_directory)
Module attributes and functions in Python:
# mymodule.py pi = 3.14 def area(radius): return pi * radius**2 # main script import mymodule circle_area = mymodule.area(5) print(circle_area)
Module namespaces in Python:
# mymodule.py variable_in_module = "I am in the module!" # main script import mymodule print(mymodule.variable_in_module)