Executing the python Script (2024)

When we speak of Python we often mean not just the language but also the implementation. Python is actually a specification for a language that can be implemented in many different ways.

Before proceeding further let us understand the difference between bytecode and machine code(native code), compiler and interpreter.

Machine Code(aka native code)
Machine code is set of instructions that directly gets executed by the CPU. Each instruction performs a very unique task, such as load or an logical operation on data in CPU memory. Almost all the high level languages such as C translate the source code into executable machine code with the help of Compilers, loaders and linkers. Every processor or processor family has its own machine code instruction set.

Executing the python Script (2)

Bytecode
Bytecode is also binary representation executed by virtual machine (not by CPU directly). The virtual machine (which is written different for different machines) converts binary instruction into a specific machine instruction. One of the language that uses the concept of Bytecode is Java.

Executing the python Script (3)

Machine Code is much faster as compared to Bytecode but Bytecode is portable and secure as compared to Machine Code.

Compiler 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 The interpreter is an alternative for implementing a programming language and does the same work as a compiler. Interpreter performs lexing, parsing and type checking similar to a compiler. But interpreter processes syntax tree directly to access expressions and execute statement rather than generating code from the syntax tree.

Cpython: The default implementation of the Python programming language is Cpython. As the name suggests Cpython is written in C language. Cpython compiles the python source code into intermediate bytecode, which is executed by the Cpython virtual machine. CPython is distributed with a large standard library written in a mixture of C and Python. CPython provides the highest level of compatibility with Python packages and C extension modules. All versions of the Python language are implemented in C because CPython is the reference implementation.
Some of the implementations which are based on CPython runtime core but with extended behavior or features in some aspects are Stackless Python, wpython, MicroPython.
Stackless Python — CPython with an emphasis on concurrency using tasklets and channels (used by dspython for the Nintendo DS)

Other Implementations
There are some other implementations of the Python language too The only implementations that are known to be compatible with a given version of the language are IronPython, Jython and PyPy.

Jython
Jython is an implementation of the Python programming language that can run on the Java platform. Jython programs use Java classes instead of Python modules .Jython compiles into Java byte code, which can then be run by Java virtual machine. Jython enables the use of Java class library functions from the Python program. Jython is slow as compared to Cpython and lacks compatibility with CPython libraries.

Executing the python Script (4)

In Python, the source is compiled into a much simpler form called bytecode. These are instructions similar in spirit to CPU instructions, but instead of being executed by the CPU, they are executed by a software called a virtual machine. (These are not VM’s that emulate entire operating systems, just a simplified CPU execution environment.) .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.

but in python we don’t have to compile program manually it is done by python for us automatically.

Here’s an example of a short Python function, and its bytecode:

The dis module in the Python standard library is the disassembler that can show you Python bytecode. It’s also the best (but not great) documentation for the bytecode itself.

Executing the python Script (5)

another way to compile this is as follow:

Executing the python Script (6)

it generates compiled file named python_source_file.cpython-36.pyc inside the __pycache__ folder in the current directory.

You can also automatically compile all Python files using the compileall module.

Executing the python Script (7)

The compilation is hidden from the user for a good reason. 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 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 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).

An important aspect of Python’s compilation to bytecode is that it’s entirely implicit. You never invoke a compiler, you simply run a .py file. The Python implementation compiles the files as needed. This is different than Java, for example, where you have to run the Java compiler to turn Java source into compiled class files. For this reason, Java is often called a compiled language, while Python is called an interpreted language. But both compile to bytecode, and then both execute the bytecode with a software implementation of a virtual machine.

Another important Python feature is its interactive prompt. You can type Python statements and have them immediately executed. This interactivity is usually missing in “compiled” languages, but even at the Python interactive prompt, your Python is compiled to bytecode, and then the bytecode is executed. This immediate execution and Python’s lack of an explicit compile step are why people call the Python executable “the Python interpreter.”

This shows just how flimsy the words “interpreted” and “compiled” can be. Like most adjectives applied to programming languages, they are thrown around as if they were black-and-white distinctions, but the reality is much subtler and complex.

Finally, how your program gets executed isn’t a characteristic of the language at all: it’s about language implementation. I’ve been talking here about Python, but this has really been a description of CPython, the usual implementation of Python, so-named because it is written in C. PyPy is another implementation, using a JIT compiler to run code much faster than CPython can.

But why Python needs both a compiler and an interpreter?

Speed. Strict interpretation is slow. Virtually every “interpreted” language actually compiles the source code into some sort of internal representation so that it doesn’t have to repeatedly parse the code. In python’s case it saves this internal representation to disk so that it can skip the parsing/compiling process next time it needs the code.

Is .pyc file (compiled bytecode) is platform independent?

Compiled Python bytecode files are architecture-independent, but VM-dependent. A .pyc file will only work on a specific set of Python versions determined by the magic number stored in the file.

Conclusion

is Python compiled? Yes. Is Python interpreted? Yes. Sorry, the world is complicated… Python as a programming language has no saying about if it’s an compiled or interpreted programming language, only the implementation of it. The terms interpreted or compiled is not a property of the language but a property of the implementation. Python program runs directly from the source code . so, Python will fall under byte code interpreted. The .py source code is first compiled to byte code as .pyc. This byte code can be interpreted (official CPython), or JIT compiled (PyPy).

Executing the python Script (2024)

FAQs

Executing the python Script? ›

To execute a Python script, first open a terminal, then navigate to the directory where the script is located, and finally, run the script using the 'python' command followed by the script's name. On Linux, consider using python3 to ensure you're using Python 3.

How to execute a Python script? ›

To run a Python script in Terminal from the command line, navigate to the script's directory and use the python script_name.py command. Redirecting output involves using the > symbol followed by a file name to capture the script's output in a file. For example, python script_name.py > output.

How Python code will execute? ›

If code is a string, then it's parsed as a suite of Python statements, which is then internally compiled into bytecode, and finally executed, unless a syntax error occurs during the parsing or compilation step. If code holds a compiled code object, then it's executed directly, making the process a bit more efficient.

How do I run a Python script as an executable? ›

To convert a Python script to a standalone executable (.exe) file using Auto PY to EXE, you can follow these steps:
  1. Step 1: Install Auto PY to EXE. ...
  2. Step 2: Run Auto PY to EXE. ...
  3. Step 3: Configure the settings. ...
  4. Step 4: Select the Compilation Mode. ...
  5. Step 5: Click “Convert .py to .exe. ...
  6. Step 6: Find the output.

How to compile a Python code? ›

The compile() method takes in the following parameters:
  1. source - a normal string , a byte string, or an AST object.
  2. filename - file from which the code is to be read.
  3. mode - exec (can take a code block with statements, class and functions ), eval (accepts single expression) or single (has a single interactive statement)

How to write Python in terminal? ›

On a Mac or Linux machine, simply open Terminal.
  1. Activate your Conda environment: ...
  2. Create a directory to store our work. ...
  3. Go into the directory: ...
  4. Create a new Python file: ...
  5. And now that you've set up our workspace, edit the mysci.py script using your favorite text editor (e.g., nano):

What happens when you run a Python script? ›

Python is an interpreted language. This means that the Python interpreter reads a line of code, executes that line, then repeats this process if there are no errors.

How to use Python step by step? ›

Your journey to learn Python starts now.
  1. Step 1: Identify What Motivates You.
  2. Step 2: Learn the Basic Syntax, Quickly.
  3. Step 3: Make Structured Projects.
  4. Step 4: Work on Python Projects on Your Own.
  5. Step 5: Keep Working on Harder Projects.
  6. Final Words.
  7. Common Python Questions (FAQs)
Apr 17, 2024

How to execute Python script without calling Python? ›

You can use a couple options on windows.
  1. Virtual shebang: #!/usr/bin/env python3.
  2. Real path shebang: #!"C:\Python33\python.exe"
  3. Use pyinstaller to generate a single file executable.
Mar 28, 2023

How to run Python in command prompt? ›

How to Run Python Program in Interactive Mode?
  1. Step 1: Open your terminal or command prompt.
  2. Step 2: Type python and press Enter to enter the Python interpreter. ...
  3. Step 3: Type your Python code directly in the interpreter and press Enter to execute each line. ...
  4. Step 4: To exit the interpreter, type exit() and press Enter.
May 5, 2023

Can I run Python code without Python? ›

Many developers grapple with this task, but there's a tool that can make this process a breeze. Like a skilled craftsman, PyInstaller is a handy utility that can seamlessly mold your Python code into a standalone executable. These executables can run on any system, even those without Python installed.

What is the keyboard command to run a Python script? ›

You can always use ctrl + f5 to run your Python code without debugging in your VS Code terminal.

How to run Python file in terminal vs code? ›

Right-click on the editor or use the run button provided on thhe left corner of VScode to run the Python file. You should see the output in the terminal at the bottom of the VSCode window.

How to use command-line in Python? ›

Python Command Line Input
  1. Python Command Line Input. ❮ Previous Next ❯ ...
  2. C:\Users\Your Name>python demo_string_input.py. Our program will prompt the user for a string:
  3. Enter your name: The user now enters a name:
  4. Linus. Then, the program prints it to screen with a little message:
  5. Hello Linus. ❮ Previous Next ❯

How to run a Python script on a Mac? ›

To run your script from the Finder you have two options:
  1. Drag it to Python Launcher.
  2. Select Python Launcher as the default application to open your script (or any .py script) through the finder Info window and double-click it. Python Launcher has various preferences to control how your script is launched.

Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 6265

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.