Execute a Python script
In the following example we will write a variation of the mandatory "Hello World" script:
>>> print "It's easy to write a Python script!" It's easy to write a Python script! >>>
The interactive interpreter is great for checking small bits of code, but if we have to
write a serious program or script, we need to save our script in a file.
Let's save our mini program in a source file. To save and edit programs in a file we need an editor. There are lots of editors, but you should choose one, which supports syntax highlighting and indentation. Under Linux you can use vi, vim, emacs, geany, gedit and umpteen others.
So, after you have chosen your editor, you can input your mini script and save it as easy_to_write.py.
The suffix .py is not really necessary under Linux but it's good style to use it. But it is essential, if you want to write modules.
Start a Python script
Let's assume our script is in a subdirectory under the home directory:
monty@python:~$ cd python monty@python:~/python$ python easy_to_write.py It's easy to write a Python script! monty@python:~/python$
there is a different way of starting Python scripts under Linux. If you want to know how to do it, you can confer this chapter for Python3.
Python InternalsMost probably you will have read somewhere, that the Python language is an interpreted programming or script language. The truth is: Python is both an interpreted and a compiled language. But calling Python a compiled language would be misleading. 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 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 doing the thinking for you.
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_compile >>> py_compile.compile('easy_to_write.py') >>>or using the following command at the shell prompt
python -m py_compile easy_to_write.pyEither way, a file named "easy_to_write.pyc" will be created.
You can also automatically compile all files in a directory 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 bother about compiling Python code. The compilation is hidden from the user. Some newbies to Python 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 suffic. 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 there exists a compiled version with the .pyc suffix. 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 exists 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 Virtual Machine (PVM).
Every time a Python script is executed, byte code is created. But only, if a Python program 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:~$ cd python monty@python:~/python$ python easy_to_write.py It's easy to write a Python script! monty@python:~/python$The import in the following session will create a byte code file with the name "easy_to_write.pyc":
monty@python:~/python$ ls easy_to_write.py monty@python:~/python$ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import easy_to_write It's easy to write a Python script! >>> monty@python:~/python$ ls easy_to_write.py easy_to_write.pyc monty@python:~/python$
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 ore machine code.
Definition: An interpreter is a computer program that executes instructions written in a programming language. It can either
- execute the source code directly or
- translates the source code in a first step into a more efficient representation and executes this code