Command to Run Script Again Python

3. Execute a Script

By Bernd Klein. Concluding modified: 01 Feb 2022.

So far nosotros have played around with Python commands in the Python shell. We desire to write now our first serious Python program. Yous will hardly find any beginner'south textbook on programming, which doesn't start with the "almost mandatory" "Hi Globe" program, i.e. a program which prints the string "Hello World". We start a Python interactive shell in a concluding with the control "python". It might be necessary to apply "python3" to go a Python3 shell:

          $ python3 (base)            [email protected]:~$ python Python 3.7.1 (default, Dec 14 2018, xix:28:38)  [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> print("Hi Earth!") Hello World! >>>        

Merely, every bit we said at the get-go, we want to write a "serious" program now, i.e. a program which resides in a file. This manner, we can use a program over and over again without having to type information technology in again. Some may like to call such a little program "script". We will utilise a slight variation of the "Howdy Earth" theme. We have to include our print role into a file. To save and edit our program in a file nosotros need an editor. In that location are lots of editors, only yous should choose 1, which supports syntax highlighting and indentation. For Linux you lot can employ vi, vim, emacs, geany, gedit and umpteen others. The Emacs works for Windows likewise, but Notepad++ may exist the better option in many cases. Of course, you can also use an IDE (Integrated Evolution Environment) similar PyCharm or Spyder for this purpose.

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

                            print              (              "My showtime simple Python script!"              )            

OUTPUT:

My first simple Python script!            

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

Start a Python plan

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

                      [email protected]:~$ cd python            [electronic mail protected]:~/python$ python my_first_simple_program.py  My outset simple Python script!            [email protected]:~/python$        

Information technology tin can be started in a Command Prompt in Windows besides (outset -> All Programs -> Accessories -> Control Prompt). You may detect that nosotros called our little programme "hello.py" and not my_first_simple_program.py:

Command Prompt

Starting a Python Script nether Windows

Python Internals

Most probably you take read somewhere that the Python language is an interpreted programming or a script language. 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 notice the definitions for Compilers and Interpreters, in case y'all are not already familiar with the concepts!) People would assume that the compiler translates the Python code into machine language. Python lawmaking is translated into intermediate lawmaking, which has to be executed by a virtual machine, known every bit the PVM, the Python Virtual Machine. This is a similar approach to the one taken past Java. There is even a fashion 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 take to compile my Python scripts to make them faster or how tin can I compile them? The answer is easy: commonly, yous don't need to do annihilation and yous shouldn't carp, because "Python" is already doing the thinking for you, i.e. it takes the necessary steps automatically.

For whatever reason you desire to compile a python program manually? No problem. It tin can exist washed with the module py_compile, either using the interpreter beat

                            import              py_compile              py_compile              .              compile              (              'my_first_simple_program.py'              )            

OUTPUT:

'__pycache__/my_first_simple_program.cpython-37.pyc'

or using the post-obit control at the beat prompt

          python -m py_compile my_first_simple_program.py        

Either way, you may notice two things: first, at that place will be a new subdirectory "__pycache__", if it doesn't already exist. You volition 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 lot can also automatically compile all Python files using the compileall module. You can exercise it from the shell prompt by running compileall.py and providing the path of the directory containing the Python files to compile:

                      [email protected]:~/python$ python -m compileall . Listing . ...        

Simply as we take said, you don't take to and shouldn't carp about compiling Python code. The compilation is subconscious from the user for a skilful reason. Some newbies wonder sometimes where these ominous files with the .pyc suffix might come up from. If Python has write-access for the directory where the Python plan resides, information technology will shop the compiled byte code in a file that ends with a .pyc suffix. If Python has no write admission, the program volition work anyway. The byte lawmaking will be produced but discarded when the programme exits. Whenever a Python program is called, Python volition 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 lawmaking, which will speed up the start up fourth dimension of the script. If there is no byte code version, Python will create the byte lawmaking before information technology starts the execution of the program. Execution of a Python programme ways execution of the byte code on the Python.

Source Code to Byte Code

Virtual Machine (PVM).

Compilation of a Python script

Every fourth dimension a Python script is executed, a byte code is created. If a Python script is imported every bit a module, the byte code volition be stored in the corresponding .pyc file. So, the following will not create a byte lawmaking file:

                      [email protected]:~/python$ python my_first_simple_program.py My first simple Python script!            [electronic mail protected]:~/python$        

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

                      [email protected]:~/tmp$ ls  my_first_simple_program.py            [email protected]:~/tmp$ python Python ii.vi.5 (r265:79063, Apr sixteen 2010, thirteen:57:41)  [GCC 4.4.iii] on linux2 Blazon "assistance", "copyright", "credits" or "license" for more than information. >>> import my_first_simple_script My first simple Python script! >>> leave()            [e-mail protected]:~/tmp$ ls my_first_simple_program.py  my_first_simple_program.pyc            [electronic mail protected]:~/tmp$        

Live Python training

instructor-led training course

Upcoming online Courses

Enrol here

Runnable scripts under Linux

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

And then far, we have started our Python scripts with

          python3 my_file.py        

on the bash command line. A Python script tin can also be started similar any other script nether 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 start line of your Python code file. Alternatively, this line tin can exist #!/usr/bin/python3, if this is the location of your Python interpreter. Instead using env every bit 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. Nevertheless, it as well suffers from the same trouble: The path to env may likewise be unlike on a per-motorcar basis. The file has to be fabricated executable: The command "chmod +ten scriptname" has to be executed on a Linux shell, east.m. bash. "chmod 755 scriptname" can also be used to make your file executable. In our instance:

                      $ chmod +10 my_first_simple_program.py        

We illustrate this in a fustigate session:

                      [electronic mail protected]:~$ more my_first_simple_script.py  #!/usr/bin/env python3 print("My first uncomplicated Python script!")            [electronic mail protected]:~$ ls -ltr my_first_simple_script.py  -rw-r--r-- i bernd bernd 63 Nov  4 21:17 my_first_simple_script.py            [email protected]:~$ chmod +x my_first_simple_script.py            [email protected]:~$ ls -ltr my_first_simple_script.py  -rwxr-xr-x ane bernd bernd 63 November  4 21:17 my_first_simple_script.py            [electronic mail protected]:~$ ./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 calculator language (the target language). In most cases compilers are used to transform source lawmaking into executable program, i.e. they translate code from high-level programming languages into low (or lower) level languages, by and large associates or machine code.

Interpreter

Definition: an interpreter is a reckoner program that executes instructions written in a programming language. It can either execute the source lawmaking straight or interpret the source code in a get-go pace into a more efficient representation and execute this code.

Live Python training

instructor-led training course

Upcoming online Courses

Enrol here

robertsoune1982.blogspot.com

Source: https://python-course.eu/python-tutorial/execute-a-script.php

0 Response to "Command to Run Script Again Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel