Have you ever wished to build executable files and quickly ship them to your users? Wouldn’t it be an achievement if your users could run your application without installing anything? Of course, it is, and that is a dream that many developers wish to fulfill. One way to get there is using Pyinstaller in the Python ecosystem.
But before we get there, we will give you a quick synopsis of what is meant by a Python program, what it can be used for, and how to write and run a program on the Raspberry Pi and finally cover the steps of creating executable files in Python.
What is a Python program?
Ideally, Python is an instrumental programming language that has lately taken over the programming market due to its easy-to-read syntax and allows programmers to use fewer lines of codes than other programming languages like assembly, kotlin, C e.t.c.
This language (Python) began as a scripting programming language for Linux systems. Python programs are likened to shell scripts in that the files contain a series of commands that the PC executes from top to bottom.
Unlike C programs, Python programs don’t need to be compiled before running them. Nevertheless, you will need to install a python interpreter on your PC to run them. A Python interpreter is a program that reads Python files and after that executes the code.
There is a possibility of running Python programs without the Python interpreter installed. There are python programs such as Py2exe or the earlier mentioned Pyinstaller that will package your code, in this case, python code, into stand-alone executable programs.
What can Python programs do?
The Python programming language can automate tasks like batch renaming and moving vast files similar to shell scripts. Additionally, it can be used just like a command line with IDLE, Python’s REPL (read, eval, print, loop function). There are more vital kinds of stuff you can achieve with Python. For instance, you can make use of Python to create programs like:
- 2D games.
- Desktop applications and utilities.
- Web applications.
- Special Graphical User Interfaces (GUIs).
- Small databases.
Python also has a diverse collection of libraries, which speeds up the development process, making Python a very versatile programming language. Talking about library entails lots of things you can think of, game programming, rendering graphics, web frameworks, scientific computing, and GUI interface.
A number, but unfortunately, not all of the things you can do in C can be done in Python. One shortcoming of Python as a programming language is that it is slower at computations compared to C. Still, its ease of use makes it an ideal language for prototyping programs and designing applications that are not computationally intensive.
How to come up with and run a program in Python
Here, we will only cover the essential knowledge you need to write and execute a Python program. However, here is a great tutorial that has well-covered everything a programmer or developer needs to know about Python in the book Learning Python 5th Ed. (O’Reilly) by Mark Lutz
Installing and Updating Python
Interestingly, python 2 and 3 come pre-installed on Raspbian OS, but if you are using other Linux distros, here is how to install and update it. To achieve the installation, run the following commands in your terminal, and you are good to go.
Run this command to install and update Python 3:
sudo apt-get install python3
To install and update Python 2, run this command:
sudo apt-get install python
Opening the Python REPL
Are you looking for a way to access the Python REPL to input Python commands just like the command line? If so, open up your terminal and enter “Python for python2 or python3,” depending on the version you are currently using. REPL is an acronym that stands for “Read Evaluate Print Loop” and refers to the interactive MicroPython prompt that is only accessible on the Pycom devices. Ideally, it is the simplest method to test out Python code and run commands.
To exit the REPL, click on Ctrl-D
Writing a Python program
Let us take a simple demonstration of creating and executing a Python program. So, we will make a simple “Hello boardbytes” program in this case. To begin with, open the nano editor and create a new file named hello-boardbytes.py by inputting this command :
sudo nano hello-boardbytes.py
After running that command, the nano editor will popup, enter the following code, then press Ctrl-X and Y to aid in exiting and saving the file:
#!/usr/bin/python print "Hello, Boardbytes!";
Note: A point to note is that you must save all python program files with a “.py” extension. You are also not limited to a specific editor but can use any editor such as Visual studio, Pycharm, Notepad++ to write the program. Nonetheless, save the file with a “.py” extension.
Running a Python program
To run the program without making the file executable, maneuver to the location where you saved your file and enter this command like in our case. But ensure you change the “hello-boardbytes.py” file name to your file name.
Create an Executable File From Python Script
This is the core part of this review besides python file creation. Making a python program executable permits you to run the program without inputting “python” before the file name during execution.
Here, we will take you through all the steps needed to accomplish the goal of creating an executable python program. So, without further ado, let us delve deeper.
To run a python program normally, you have to instruct the python software to open the file. Nonetheless, it is possible to execute the file without calling upon Python first. This, in turn, allows you to call your programs, that is, those you created in Python at the terminal, by just typing its name.
However, you first need to tell your machine to make your Python file executable, meaning that the file is a program. For this instance, the target file “file-name” needs to be made executable. Note that this name is a randomly picked one; however, in your case, you will have to replace this with your file name. We will then use the chmod +x command to make the file executable. As such, type the following syntax in the terminal.
chmod +x file-name.py
At this point now, you can try running the program directly on the terminal by typing the following syntax:
You will then realize that even though you did not call upon Python, the program could still run the same as typed python file-name.py. Another essential point is that the program can only be run by calling it with its full path, sometimes referred to as location home/pi/file-name.py or from the current directory using ./ as the location.
Do you need to access the files on your pi machine? You need not tire yourself up since it is very straightforward. To make the files accessible in the same way as any other command in the terminal, it needs to be copied using the cp command to the usr/local/bin with the following syntax:
sudo cp file-name.py /usr/local/bin/
The file location in the usr/local/bin can be accessed from any relevant directory by typing its name. You can try out the flexibility of this command by changing the file-name.py file to another directory and then try rerunning the program by typing the following syntax:
To make your customized programs seem more like native utilities, you can rename (by using the mv command) to remove the .py file extension. To change file-name.py in this manner, type the following syntax in your terminal;
sudo mv /usr/local/bin/file-name.py /usr/local/bin/file-name
To this extent, the program can now be run by simply typing your “file-name” in the terminal.
We believe that to this far, you by now have all your questions answered. This article has gone through the steps needed for you to create a python file typically, how to run it, and later showed you how to make python programs executable such that you only type your file name in your terminal to run it.