3. Execute a Script | Python Tutorial (2024)

By Bernd Klein. Last modified: 01 Feb 2022.

On this page

So far we have played around with Python commands in the Python shell. We want to write now our first serious Python program. You will hardly find any beginner's textbook on programming, which doesn't start with the "almost mandatory" "Hello World" program, i.e. a program which prints the string "Hello World". We start a Python interactive shell in a terminal with the command "python". It might be necessary to use "python3" to get a Python3 shell:

$ python3(base) bernd@moon:~$ pythonPython 3.7.1 (default, Dec 14 2018, 19:28:38) [GCC 7.3.0] :: Anaconda, Inc. on linuxType "help", "copyright", "credits" or "license" for more information.>>> print("Hello World!")Hello World!>>>

But, as we said at the beginning, we want to write a "serious" program now, i.e. a program which resides in a file. This way, we can use a program over and over again without having to type it in again. Some may like to call such a little program "script". We will use a slight variation of the "Hello World" theme. We have to include our print function into a file. To save and edit our program in a file we need an editor. There are lots of editors, but you should choose one, which supports syntax highlighting and indentation. For Linux you can use vi, vim, emacs, geany, gedit and umpteen others. The Emacs works for Windows as well, but Notepad++ may be the better choice in many cases. Of course, you can also use an IDE (Integrated Development Environment) like PyCharm or Spyder for this purpose.

So, after you have found the editor or the IDE of your choice, you are ready to develop your mini program, i.e.

print("My first simple Python script!")

OUTPUT:

My first simple Python script!

and save it as my_first_simple_program.py. The suffix .py is not really necessary for Linux but it's good to use it since the extension is essential, if you want to write modules.

Start a Python program

Let's assume our script is in a subdirectory under the home directory of the user monty:

monty@python:~$ cd pythonmonty@python:~/python$ python my_first_simple_program.py My first simple Python script!monty@python:~/python$

It can be started in a Command Prompt in Windows as well (start -> All Programs -> Accessories -> Command Prompt). You may notice that we called our little program "hello.py" and not my_first_simple_program.py:

3. Execute a Script | Python Tutorial (1)

Starting a Python Script under Windows

Python Internals

Most probably you have read somewhere that the Python language is an interpreted programming or a scriptlanguage. The truth is: Python is both an interpreted and a compiled language. But calling Python a compiled language would be misleading. (At the end of this chapter, you will find the definitions for Compilers and Interpreters, in case you are not already familiar with the concepts!) People would assume that the compiler translates the Python code into machine language. Python code is translated into intermediate code, which has to be executed by a virtual machine, known as the PVM, the Python Virtual Machine. This is a similar approach to the one taken by Java. There is even a way of translating Python programs into Java byte code for the Java Virtual Machine (JVM). This can be achieved with Jython.

The question is, do I have to compile my Python scripts to make them faster or how can I compile them? The answer is easy: normally, you don't need to do anything and you shouldn't bother, because "Python" is already doing the thinking for you, i.e. it takes the necessary steps automatically.

For whatever reason you want to compile a python program manually? No problem. It can be done with the module py_compile, either using the interpreter shell

import py_compilepy_compile.compile('my_first_simple_program.py')

OUTPUT:

'__pycache__/my_first_simple_program.cpython-37.pyc'

or using the following command at the shell prompt

python -m py_compile my_first_simple_program.py

Either way, you may notice two things: first, there will be a new subdirectory "__pycache__", if it doesn't already exist. You will find a file "my_first_simple_script.cpython-34.pyc" in this subdirectory. This is the compiled version of our file in byte code.

You can also automatically compile all Python files using the compileall module. You can do it from the shell prompt by running compileall.py and providing the path of the directory containing the Python files to compile:

monty@python:~/python$ python -m compileall .Listing . ...

But as we have said, you don't have to and shouldn't bother about compiling Python code. The compilation is hidden from the user for a good reason. Some newbies wonder sometimes where these ominous files with the .pyc suffix might come from. If Python has write-access for the directory where the Python program resides, it will store the compiled byte code in a file that ends with a .pyc suffix. If Python has no write access, the program will work anyway. The byte code will be produced but discarded when the program exits. Whenever a Python program is called, Python will check, if a compiled version with the .pyc suffix exists. This file has to be newer than the file with the .py suffix. If such a file exists, Python will load the byte code, which will speed up the start up time of the script. If there is no byte code version, Python will create the byte code before it starts the execution of the program. Execution of a Python program means execution of the byte code on the Python.

3. Execute a Script | Python Tutorial (3)

Virtual Machine (PVM).

Compilation of a Python script

Every time a Python script is executed, a byte code is created. If a Python script is imported as a module, the byte code will be stored in the corresponding .pyc file. So, the following will not create a byte code file:

monty@python:~/python$ python my_first_simple_program.pyMy first simple Python script!monty@python:~/python$

The import in the following Python2 session will create a byte code file with the name "my_first_simple_program.pyc":

monty@python:~/tmp$ ls my_first_simple_program.pymonty@python:~/tmp$ pythonPython 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import my_first_simple_scriptMy first simple Python script!>>> exit()monty@python:~/tmp$ lsmy_first_simple_program.py my_first_simple_program.pycmonty@python:~/tmp$

Live Python training

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Upcoming online Courses

Enrol here

Runnable scripts under Linux

This chapter can be skipped by Windows users. Don't worry! It is not essential!

So far, we have started our Python scripts with

python3 my_file.py

on the bash command line. A Python script can also be started like any other script under Linux, e.g. Bash scripts. Two steps are necessary for this purpose: the shebang line #!/usr/bin/env python3 has to be added as the first line of your Python code file. Alternatively, this line can be #!/usr/bin/python3, if this is the location of your Python interpreter. Instead using env as in the first shebang line, the interpreter is searched for and located at the time the script is run. This makes the script more portable. Yet, it also suffers from the same problem: The path to env may also be different on a per-machine basis. The file has to be made executable: The command "chmod +x scriptname" has to be executed on a Linux shell, e.g. bash. "chmod 755 scriptname" can also be used to make your file executable. In our example:

 $ chmod +x my_first_simple_program.py

We illustrate this in a bash session:

bernd@marvin:~$ more my_first_simple_script.py #!/usr/bin/env python3print("My first simple Python script!")bernd@marvin:~$ ls -ltr my_first_simple_script.py -rw-r--r-- 1 bernd bernd 63 Nov 4 21:17 my_first_simple_script.pybernd@marvin:~$ chmod +x my_first_simple_script.py bernd@marvin:~$ ls -ltr my_first_simple_script.py -rwxr-xr-x 1 bernd bernd 63 Nov 4 21:17 my_first_simple_script.pybernd@marvin:~$ ./my_first_simple_script.py My first simple Python script!

Differences between Compilers and Interpreters

Compiler

Definition: a compiler is a computer program that transforms (translates) source code of a programming language (the source language) into another computer language (the target language). In most cases compilers are used to transform source code into executable program, i.e. they translate code from high-level programming languages into low (or lower) level languages, mostly assembly or machine code.

Interpreter

Definition: an interpreter is a computer program that executes instructions written in a programming language. It can either execute the source code directly or translate the source code in a first step into a more efficient representation and execute this code.

Live Python training

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Upcoming online Courses

Enrol here

3. Execute a Script | Python Tutorial (2024)
Top Articles
Volkswagen T-Roc vs Volkswagen Tiguan
Why Do Bitcoins Have Value?
11 beste sites voor Word-labelsjablonen (2024) [GRATIS]
It's Official: Sabrina Carpenter's Bangs Are Taking Over TikTok
Napa Autocare Locator
Jefferey Dahmer Autopsy Photos
Die Windows GDI+ (Teil 1)
Mail Healthcare Uiowa
Big Y Digital Coupon App
Www Thechristhospital Billpay
How to Watch Braves vs. Dodgers: TV Channel & Live Stream - September 15
Natureza e Qualidade de Produtos - Gestão da Qualidade
R/Altfeet
Aces Fmc Charting
50 Shades Darker Movie 123Movies
Vermont Craigs List
Wausau Obits Legacy
Bridge.trihealth
Outlet For The Thames Crossword
Aldi Bruce B Downs
Dragonvale Valor Dragon
Village
Yosemite Sam Hood Ornament
Filthy Rich Boys (Rich Boys Of Burberry Prep #1) - C.M. Stunich [PDF] | Online Book Share
Best Boston Pizza Places
Makemv Splunk
The Boogeyman (Film, 2023) - MovieMeter.nl
Uncovering the Enigmatic Trish Stratus: From Net Worth to Personal Life
Co10 Unr
Housing Intranet Unt
35 Boba Tea & Rolled Ice Cream Of Wesley Chapel
"Pure Onyx" by xxoom from Patreon | Kemono
47 Orchid Varieties: Different Types of Orchids (With Pictures)
Where Can I Cash A Huntington National Bank Check
Beaver Saddle Ark
De beste uitvaartdiensten die goede rituele diensten aanbieden voor de laatste rituelen
Navigating change - the workplace of tomorrow - key takeaways
Great Clips On Alameda
D3 Boards
When His Eyes Opened Chapter 2048
303-615-0055
Ukraine-Krieg - Militärexperte: "Momentum bei den Russen"
Miami Vice turns 40: A look back at the iconic series
Disassemble Malm Bed Frame
Brown launches digital hub to expand community, career exploration for students, alumni
American Bully Puppies for Sale | Lancaster Puppies
View From My Seat Madison Square Garden
Saw X (2023) | Film, Trailer, Kritik
Cbs Scores Mlb
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated:

Views: 6154

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.