Standalone Python Program Part I

I am back from 4th ICT! Okay, that’s quite irrelevant, so let’s save it for later: this short post series will show 1. how a stand alone program can be built from python files and 2. my final year project sideshow, as the example.

How is your first programming experience? You probably got asked to write a script and print something like, “Hello world”, and you did, and you saw “Hello world” somewhere in that something you used. The something you used is probably an IDE, integrated development environment. It is a cool thing, it helps you organise your files, some have the auto-correct equivalent of the programming world (IntelliSense etc), and even helps you refactor your code, i.e. when you change name of say variable myinteger to someinteger, you do not need to change all the “myinteger”s in the long code: they do it for you.

All is good and fancy-sounding, but wait, we haven’t done anything. Here, we use pycharm. Create new project, name it, say “myproject”, right-click myproject with the folder icon, then add these 2 new python scripts

main.py

from sidefile import *

print("Hello world")
test_function()
input("Type in something:\n")

sidefile.py

def test_function():
    print("some function")
    return

and then Run > Run or press Alt+Shift+F10. In the console, we see that

Hello world
some function
Type in something:

is printed. All is fine, and we can type in something as requested by the last line. But as an inquisitive coder you were still wondering how do people build any standalone program? Like how does a program like Microsoft Word, or perhaps Steam, or perhaps a music player exist as a standalone program, i.e. it does not just exist inside an IDE? Yes, you need to do something to the python files, and such standalone program is what we exactly want to build here.

First, open a command line that can use python. In this example, we use PowerShell. The following three lines do the following (1) Download pyinstaller and then (2) move into myproject folder (wherever it is). And then we will be (3) using the pyinstaller to build the standalone program. In my case we enter the following lines in PowerShell:

pip install pyinstaller
cd Desktop/myproject
pyinstaller main.py

Check out the folder myfolder/main/dist. There will be main.exe. Double-click it, and we have a very simple standalone program!

In part II, I will demonstrate how this is used to compile and build a more complex standalone program.

Some extra stuffs

We use and assume some familiarities with:

  1. python 3.5.1, downloaded along with Anaconda
  2. pycharm community
  3. Windows 10

What is ICT mentioned at the start of this post? It is the in-camp training of the National Service in Singapore. We Singaporeans serve two years in the army, and, upon completion, have to serve 10 more times in the following years. For my vocation, I have gone back every year since my undergraduate study year 1 summer break. It seems like I will have to go every year after this *sigh*. Alright, it can be tiring at times, but there were fun times too!