Salvatore Lopiparo

Software Engineer

Super Simple Python #10 – Modules and Importing

If you find a bug in a program that is several months old, and you can’t understand the code enough to find or fix the bug, you have a big problem. The second most important part of programming, after making the code work, is making the code easy to understand. There will be several topics about ease-of-understanding tips and tricks, and our first installment will be about modules.

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

Oh, this topic is about modules? I guess I should start then.

A module is a Python term for a file that contains Python code, or a .py file. The file we made last lesson (lesson_9.py) is a module. We can use the functions (greet, adder and main) without redefining them by importing the module!

>>> import lesson_9

We can now access the functions in our lesson_9 module by using the format: module.function(). Here’s our module and function:

>>> lesson_9.greet('Rhonin')
Hello Rhonin! Nice to meet you!

This lets us keep our code organized by putting related code together in the same module without cluttering it up!

There are several built-in modules in Python. In fact, there are a whole library of them! I recommend looking there for all your module needs! Here’s a few examples:

>>> import random  # For getting random numbers
>>> import urllib  # For accessing website urls
>>> import tkinter  # A GUI module
>>> import sys  # System functions
>>> import os  # Operating-System interface functions
>>> import os.path  # Functions for navigating the file system

Let me know in the comments if you want to see one of these modules! Each one can do some pretty cool stuff!

Super Simple Python #9 – Running a Python Script

After this lesson, you will be able to make your very own Python scripts! You’ll be a fully fledged Programmer! (Well, kinda…)

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

First, we need to create the file that will contain our script. In PyCharm, select our “super_simple_tutorial” project that we created in Lesson #0. Next, go to File > New > Python File. We normally name modules (a.k.a. Python files) something related to what it will contain, so lets name it lesson_9.py. Mine now looks like this:

__author__ = 'TorelTwiddler'

Not much there at the moment. You can leave or remove the __author__ = 'TorelTwiddler' line if you want, it won’t affect the rest of our script.

Now let’s put something useful in our module, like a function or two!

def greeter(name):
    print("Hello " + name + "! Nice to meet you!")


def adder(x, y):
    print("Adding " + str(x) + " to " + str(y) + "...")
    return x + y

We now have the functions defined in our module, but we still need to do something with them. That’s when we define the main function. The main function is the entrance point to your script. It’s where you set up anything you need before execution of the interesting stuff happens.

def main():
    greet("Thrall")
    apples = 3
    bananas = 4
    fruit = adder(apples, bananas)
    print("My fruit: " + str(fruit))

At the bottom of your file, we need to tell the Python Interpreter that if this file is the “main” entry point, to execute the main function. I will go into more detail about this is a later lesson. For now, put this at the bottom of the file:

if __name__ == "__main__":
    main()

Hurray! You now have a full Python script! Now to run the file, we’ll need to run a command shell (aka terminal). Luckily PyCharm has one built-in!

In PyCharm, go to Tools > Open Terminal. A new window will pop up in place of our Python Console (if it was open). It should look something like this:

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\TorelTwiddler\PycharmProjects\super_simple_tutorial>

Note: You can also get to the operating system Terminal or Command Shell from one of the following: Windows – Start > search for Command Prompt; Mac – Finder > search for Terminal; Linux GUI – search button > Terminal; Linux Terminal – wat?

Now that we have our terminal, let’s run our program. To run a Python script in a terminal, we have to tell Python which script to run. We can use python [script_path] to execute a script. Here is how we run our script:

C:\Users\TorelTwiddler\PycharmProjects\super_simple_tutorial>python lesson_9.py
Hello Thrall! Nice to meet you!
Adding 3 to 4...
My fruit: 7

C:\Users\TorelTwiddler\PycharmProjects\super_simple_tutorial>