Python’s Import System Explained

It’s like a secret handshake for programmers!

First: why do we need this? Well, imagine you have two files in your project: `my_program.py` and `useful_functions.py`. In `my_program.py`, you want to use some of the functions from `useful_functions.py`. Instead of copying-pasting those functions into `my_program.py`, we can just import them!

So, how do we do this? Let’s say that our project is in a folder called “project”. Here are the steps:

1. Make sure both files (`my_program.py` and `useful_functions.py`) are in your “project” folder.
2. Open up `my_program.py`.
3. Add this line at the top of the file:

# Import the useful_functions module
import useful_functions

# Define a function called main
def main():
    # Call the greet function from the useful_functions module
    useful_functions.greet()

# Call the main function
main()

# Output: "Hello, welcome to my program!"

# The above code imports the useful_functions module and defines a main function.
# The main function calls the greet function from the useful_functions module.
# This will output the greeting "Hello, welcome to my program!".

# The following code is not necessary as it is already imported in the first line.
# import useful_functions

# Define a function called main
def main():
    # Call the add_numbers function from the useful_functions module
    useful_functions.add_numbers(5, 10)

# Call the main function
main()

# Output: 15

# The above code defines a main function and calls the add_numbers function from the useful_functions module.
# The add_numbers function takes two arguments, 5 and 10, and returns their sum, which is then outputted.
# This will output the number 15.

4. Save and run!

That’s it! Now you can use any function from `useful_functions` in your code by calling its name, like so:

# Import the useful_functions module
import useful_functions

# Call the my_function function from the useful_functions module
# and assign the result to the variable result
result = useful_functions.my_function(arg1, arg2) # arg1 and arg2 are the arguments passed to the function

# Print the result
print(result) # prints the result of the function call

# The script imports a module called useful_functions and calls a function called my_function from that module. 
# The result of the function call is assigned to the variable result and then printed.

But wait what if the file we want to import is not in our project folder? No problem! Let’s say that `useful_functions` is actually a package (a collection of files and modules). Here are some steps for using packages:

1. Make sure both your program and the package you want to use are installed on your computer.
2. Open up `my_program.py`.
3. Add this line at the top of the file (assuming that the package is called “useful”):

# Import the "useful" package
import useful

# This line imports the "useful" package, which is a collection of files and modules that we want to use in our program.

# Make sure both our program and the package are installed on our computer
# This step is important because we need to have both our program and the package installed in order to use it.

# Open up "my_program.py"
# This step assumes that we have a file called "my_program.py" that we want to add the "useful" package to.

# Add this line at the top of the file
# This line adds the "useful" package to our program, allowing us to use its functions and modules within our code.
import useful

4. Save and run!

That’s it again! Now you can use any function from `useful` in your code by calling its name, like so:

# Import the useful module
import useful

# Define two variables with values
arg1 = 5
arg2 = 10

# Call the my_function function from the useful module and assign the result to the result variable
result = useful.my_function(arg1, arg2)

# Print the result
print(result)

# Output: 15

# The above code imports the useful module, defines two variables with values, calls the my_function function from the useful module with the two variables as arguments, and prints the result. The result should be 15, as the function adds the two arguments together.

But wait what if the package is not installed on my computer? No problem again! Let’s say that you want to use a package called “requests” (which allows us to send HTTP requests). Here are some steps for installing packages:

1. Open up your terminal or command prompt.
2. Navigate to the folder where `my_program.py` is located.
3. Run this command (assuming that you have pip installed):

# This script installs the "requests" package using pip, which allows us to send HTTP requests.

# Navigate to the folder where `my_program.py` is located.
cd /path/to/my_program

# Install the "requests" package using pip.
pip install requests

# Note: pip is a package manager for Python, and it allows us to easily install and manage packages.

# Note: The "requests" package is used to make HTTP requests, which are used to communicate with web servers.

# Note: The `cd` command is used to change directories in the terminal or command prompt.

# Note: The `pip install` command is used to install packages using pip.

# Note: The `requests` package is specified after the `pip install` command to indicate which package we want to install.

4. Wait for it to finish downloading and installing!
5. Open up `my_program.py`.
6. Add this line at the top of the file:

# Import the requests library, which allows us to make HTTP requests
import requests

# Make a GET request to download and install the program
response = requests.get("https://www.example.com/my_program.py")

# Check if the request was successful
if response.status_code == 200:
    # If successful, open the downloaded program
    with open("my_program.py", "w") as file:
        # Write the contents of the response to the file
        file.write(response.text)

# Add a line at the top of the file to indicate it has been modified
print("This file has been modified.")

# The program is now ready to be used!

7. Save and run!

That’s it again now you can use any function from “requests” in your code by calling its name, like so:

# Import the "requests" library to make HTTP requests
import requests

# Use the "get" function from the "requests" library to make a GET request to the specified URL
response = requests.get("https://www.google.com")

# Print the response from the GET request
print(response)

# The response variable now contains the response from the GET request, which can be used for further processing or analysis.

And that’s all there is to Python’s import system! It may seem complicated at first, but once you get the hang of it, it becomes second nature.

SICORPS