Now, I know what you might be thinking: Why would anyone want to do that? Isn’t it easier just to copy and paste code into your project? Well, my friend, let me tell you something. Copy-pasting is for amateurs! Real Python programmers use the import protocol.
So, without further ado, lets dive right in!
Step 1: Create a local folder with some modules inside it.
Let’s say we have a project called “my_project” and within that project, there is another folder named “local_modules”. Inside this folder, we will create two Python files `mathutils.py` and `stringutils.py`. These are the modules we want to import from our local folder.
Step 2: Write some code in your main script (let’s call it `main.py`) that uses these imported modules.
Let’s say you have a function called “calculate_distance” which needs to use mathutils module, and another function called “reverse_string” which requires stringutils module. Here is what the code might look like:
# Importing necessary modules
from .local_modules import mathutils, stringutils
# Defining a function to calculate distance between two points
def calculate_distance(x1, y1, x2, y2):
""" Calculates distance between two points using Pythagoras' theorem"""
# Importing the distance function from mathutils module
from mathutils import distance
# Calculating the distance using Pythagoras' theorem
distance = round(math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2), 2)
# Returning the calculated distance
return distance
# Defining a function to reverse a given string
def reverse_string(input_str):
""" Reverses a given string using list comprehension"""
# Importing the reversed_string function from stringutils module
from stringutils import reversed_string
# Reversing the given string using list comprehension
reversed_str = [char for char in input_str[::-1]]
# Returning the reversed string
return reversed_str
Step 3: Run your code!
Now, you can run `main.py` and use the imported functions as if they were part of Python’s standard library.
But wait a minute… what’s that? You don’t see any output?! That’s because we forgot to add some print statements in our main script! Let’s fix that:
# Importing necessary modules
from .local_modules import mathutils, stringutils
# Defining a function to calculate distance between two points
def calculate_distance(x1, y1, x2, y2):
""" Calculates distance between two points using Pythagoras' theorem"""
# Importing the distance function from mathutils module
from mathutils import distance
# Calculating the distance using Pythagoras' theorem
return round(math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2), 2)
# Defining a function to reverse a given string
def reverse_string(input_str):
""" Reverses a given string using list comprehension"""
# Importing the reversed_string function from stringutils module
from stringutils import reversed_string
# Reversing the string using list comprehension
return reversed_string(input_str)
# Checking if the script is being run as the main script
if __name__ == '__main__':
# Defining the coordinates of two points
x1 = 5
y1 = 7
x2 = 9
y2 = 3
# Defining an input string
input_str = "hello world"
# Printing the distance between the two points using the calculate_distance function
print("Distance between (", x1, ",", y1, ") and (", x2, ",", y2, ") is:", calculate_distance(x1, y1, x2, y2))
# Printing the reversed string using the reverse_string function
print("Reversed string: ", reverse_string(input_str))
# Output:
# Distance between ( 5 , 7 ) and ( 9 , 3 ) is: 5.66
# Reversed string: dlrow olleh
You’ve successfully imported modules from a local folder using Python’s import protocol. Now go out and impress your friends with your newfound knowledge!