Salvatore Lopiparo

Software Engineer

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>

DROP A COMMENT

Your email address will not be published. Required fields are marked *