Python Modules
In Python, a module is a file that contains definitions of functions, classes, and variables, and can be imported into other Python files or programs. Modules are used to organize code and make it more reusable and easier to maintain.
Using modules is a good way to organize and reuse your code, and it's a common practice in Python programming.
We are going to learn how to import functions from other modules (files) in the following steps:
Create a python file named circle.py
, then add the following code to the file
Create a python file named rectangle.py
, then add the following code to the file
Now create a new file names main.py
. This is our main file and our program execution would begin from here.
We will import the circle and rectangle files using the import
statement
Working with module files and subdirectories
When working with multiple directories (folders), accessing your modules would be a quite different approach
Create a finder directory (folder) within your current directory
Create a
sayHelloy.py
andsayHi.py
file.
Within the sayHi.py
, paste the following code:
Within the sayHello.py
, paste the following code:
Now, let's access the sayHello
and sayHi
functions within those files from our main file using the following block of code:
Last updated