Modules and packages are a way to organize and structure your Python code. They allow you to reuse code, split your code into smaller manageable chunks, and organize it in a logical way.
- Using Modules: A module is a single file containing Python definitions and statements. You can use the import statement to import a module and use its functions and variables. For example, let’s say you have a file called mymodule.py which contains the following code:
You can use this module in another script by using the import statement:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTH0i7oE24t4_VqYfNmaH26RBr88Uj1RgLipWAQFpZ4vLvK70pg7EmV5KyID0MZf3D3DA1kjR8EsbzcuVlQFcdbHqW5ZiVqWeHzN-wpmr4ZHvZySkkNrOpgRAbSnIzlpaXnHx1k1mlrAeFJQwmMMojRBO1x6LznXEoifAdUjxSOmg-tET_iJi0bhVR/w640-h266/programmingdoor.com_pythonprogramming27.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTH0i7oE24t4_VqYfNmaH26RBr88Uj1RgLipWAQFpZ4vLvK70pg7EmV5KyID0MZf3D3DA1kjR8EsbzcuVlQFcdbHqW5ZiVqWeHzN-wpmr4ZHvZySkkNrOpgRAbSnIzlpaXnHx1k1mlrAeFJQwmMMojRBO1x6LznXEoifAdUjxSOmg-tET_iJi0bhVR/w640-h266/programmingdoor.com_pythonprogramming27.png)
You can also use the from keyword to import a specific function or variable from a module:
- Creating your own modules and packages: You can create your own modules by creating a new file with a .py extension and writing your code in it. You can then use the import statement to use this code in other scripts. To create your own package, you need to create a new folder and put your modules inside it.
You also need to create an __init__.py file inside the folder to indicate that it is a package. For example, let’s say you have two modules module1.py and module2.py inside a folder called mypackage. The __init__.py file should be empty but it needs to be there for python to treat the folder as a package.
You can then use the package by importing specific modules from it:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk3Um7vuSBO3wXev1yAX1RNc217hZ_f3Iq1wvmKNP34DxjCUdDwGPlBzLy_rDQPeTvnas8BtFM4tCA7bsyIkC1gkpaTRJoH1CTZyN3PeQpDj9Wr-UhYAUkU1JkNMynYRo5rpOW7z3-UBoJNBHd_cU0NNYItsHJAJL-XkK4qPzIb_wV2aI94NMb0IK-/w640-h362/programmingdoor.com_pythonprogramming30.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk3Um7vuSBO3wXev1yAX1RNc217hZ_f3Iq1wvmKNP34DxjCUdDwGPlBzLy_rDQPeTvnas8BtFM4tCA7bsyIkC1gkpaTRJoH1CTZyN3PeQpDj9Wr-UhYAUkU1JkNMynYRo5rpOW7z3-UBoJNBHd_cU0NNYItsHJAJL-XkK4qPzIb_wV2aI94NMb0IK-/w640-h362/programmingdoor.com_pythonprogramming30.png)
- Commonly used built-in modules: There are many built-in modules in Python that provide a wide range of functionality. Some of the most commonly used ones are:
- os: This module provides a way to interact with the operating system. You can use it to access environment variables, manipulate files and directories, and more.
- sys: This module provides access to system-specific parameters and functions. You can use it to access command-line arguments, exit the program, and more.
- time: This module provides functions to work with time and dates. You can use it to get the current time, measure the time taken for a specific operation, and more.
- math: This module provides mathematical functions and constants. You can use it to perform mathematical operations such as trigonometry, logarithms, and more.
- random: This module provides functions to generate random numbers and perform random operations. You can use it to generate random numbers, shuffle lists, and more.
These are just a few examples, Python has many more built-in modules and third-party libraries to perform a wide range of tasks. You Just have to explore more and more.