How to run a Python program
There are a few different ways in which you can run a Python program.
Running Python scripts
Python can be used as a scripting language; in fact, it always proves itself very useful. Scripts are files (usually of small dimensions) that you normally execute to do something like a task. Many developers end up having an arsenal of tools that they use when they need to perform a task. For example, you can have scripts to parse data in a format and render it into another one, or you can use a script to work with files and folders; you can create or modify configuration files—technically, there is not much that cannot be done in a script.
It is rather common to have scripts running at a precise time on a server. For example, if your website database needs cleaning every 24 hours (for example, to regularly clean up expired user sessions), you could set up a Cron job that fires your script at 1:00 A.M. every day.
According to Wikipedia, the software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use Cron (or a similar technology) to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.
We have Python scripts to do all the menial tasks that would take us minutes or more to do manually, and at some point, we decided to automate.
Running the Python interactive shell
Another way of running Python is by calling the interactive shell. This is something we saw when we typed python
in the command line of our console.
So, open up a console, activate your virtual environment (which by now should be second nature to you, right?), and type python
. You will be presented with a few lines that should look something like this:
(lpp4ed) fab@m1 ~/code/lpp4ed$ python
Python 3.12.2 (main, Feb 14 2024, 14:16:36)
[Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more
information.
>>>
Those >>>
are the prompt of the shell. They tell you that Python is waiting for you to type something. If you type a simple instruction, something that fits in one line, that is all you will see. However, if you type something that requires more than one line of code, the shell will change the prompt to ...
, giving you a visual clue that you are typing a multiline statement (or anything that would require more than one line of code).
Go on, try it out; let us do some basic math:
>>> 3 + 7
10
>>> 10 / 4
2.5
>>> 2 ** 1024
179769313486231590772930519078902473361797697894230657273430081157
732675805500963132708477322407536021120113879871393357658789768814
416622492847430639474124377767893424865485276302219601246094119453
082952085005768838150682342462881473913110540827237163350510684586
298239947245938479716304835356329624224137216
The last operation is showing you something incredible. We raise 2
to the power of 1024
, and Python handles this task with no trouble at all. Try to do it in Java, C++, or C#. It will not work, unless you use special libraries to handle such big numbers.
We use the interactive shell every day. It is extremely useful to debug very quickly; for example, to check if a data structure supports an operation, or to inspect or run a piece of code. You will find that the interactive shell soon becomes one of your dearest friends on this journey you are embarking on.
Another solution, which comes in a much nicer graphic layout, is to use the Integrated Development and Learning Environment (IDLE). It is quite a simple Integrated Development Environment (IDE), which is intended mostly for beginners. It has a slightly larger set of capabilities than the bare interactive shell you get in the console, so you may want to explore it. It comes for free in the Windows and macOS Python installers, and you can easily install it on any other system. You can find more information about it on the Python website.
Guido Van Rossum named Python after the British comedy group Monty Python, so it is rumored that the name IDLE was chosen in honor of Eric Idle, one of Monty Python’s founding members.
Running Python as a service
Apart from being run as a script, and within the boundaries of a shell, Python can be coded and run as an application. We will see examples throughout this book of this mode. We will look at it in more depth in a moment, when we talk about how Python code is organized and run.
Running Python as a GUI application
Python can also be used to create Graphical User Interfaces (GUIs). There are several frameworks available, some of which are cross-platform, and some others that are platform-specific. A popular example of a GUI application library is Tkinter, which is an object-oriented layer that lives on top of Tk
(Tkinter means Tk interface).
Tk is a GUI toolkit that takes desktop application development to a higher level than the conventional approach. It is the standard GUI for Tool Command Language (Tcl), but also for many other dynamic languages, and it can produce rich native applications that run seamlessly on Windows, Linux, macOS, and more.
Tkinter comes bundled with Python; therefore, it gives the programmer easy access to the GUI world.
Other widely used GUI frameworks include:
- PyQT/PySide
- wxPython
- Kivy
Describing them in detail is outside the scope of this book, but you can find all the information you need on the Python website: https://docs.python.org/3/faq/gui.html.
Information can be found in the What GUI toolkits exist for Python? section. If GUIs are what you are looking for, remember to choose the one you want according to some basic principles. Make sure they:
- Offer all the features you may need to develop your project
- Run on all the platforms you may need to support
- Rely on a community that is as wide and active as possible
- Wrap graphic drivers/tools that you can easily install/access