There is no "Python Virtual Machine", really. The python executable contains a bytecode compiler and a bytecode interpreter, but those are an implementation detail - you can have a Python implementation that compiles your code ahead of time, like Nuitka, or one using JIT compilation, like PyPy, or one that does not compile anything at all, like MicroPython. Answer from K900_ on reddit.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-virtual-machine
Python Virtual Machine - GeeksforGeeks
July 23, 2025 - The Python Virtual Machine, often referred to as the Python interpreter, is responsible for executing Python code. It serves as an abstraction layer between the Python bytecode and the underlying hardware, providing a consistent environment ...
🌐
Reddit
reddit.com › r/learnpython › can someone explain the interaction between python virtual machine and running the python command in the terminal?
r/learnpython on Reddit: Can someone explain the interaction between Python Virtual Machine and running the Python command in the terminal?
April 11, 2022 -

So from my understanding, the Python Virtual Machine takes what you wrote in a py file, compiles it into bytecode either in memory or on disk as pyc files.

This is then interpreted line by line by the same Python Virtual Machine as machine code which is then executed by your computer line by line.

What exactly is a Python Virtual Machine in practice? Where am I seeing it?

Is it like when I open a terminal, type python, and it puts me in some other mode where I can't use commands like "ls" and "cd" anymore. Am I "inside the Python Virtual Machine"? So is the "Python Virtual Machine" just some executable file that the terminal can run?

I think this is referred to running Python in "interactive mode". So since it isn't probably creating a pyc file everytime I type something, is it still doing that in memory when I type some command? So it has a million different "pyc"s in memory since it's creating a new one everytime I write a line in the terminal since it needs to convert to bytecode before actually then being able to convert to machine code?

Top answer
1 of 5
10
There is no "Python Virtual Machine", really. The python executable contains a bytecode compiler and a bytecode interpreter, but those are an implementation detail - you can have a Python implementation that compiles your code ahead of time, like Nuitka, or one using JIT compilation, like PyPy, or one that does not compile anything at all, like MicroPython.
2 of 5
2
The Python Virtual Machine (PVM) is a subsystem of the Python executable that converts Python bytecode (the compiled binary content that is held in either memory or cached as .pyc files) to executable machine code. In other words it receives bytecode and converts it to something that can actually be run directly by the physical hardware, therefore it’s the very last step in converting Python code to actual machine action. The first step is parsing Python code you’ve written, which you’ll provide either via an interactive interpreter session (known as a Read Eval Print Loop or REPL) which you’ll enter by invoking python without arguments, or via a .py file already written using python file.py or via a module already written that’s on Python’s search path via python -m modulename. In all three cases the parser (another subsystem of the Python executable) will take your code and, assuming it’s valid syntactically, turn it into what’s known as an Abstract Syntax Tree (AST). An AST is a very explicit, if very verbose, representation of your code, and each node in the AST equates to specific instructions to emit as bytecode. These instructions have a human-readable representation (you can use the dis module to learn more about these), but will usually be compiled directly to binary bytecode in memory. Once all the bytecode is compiled the program can now be handed to the PVM to actually be run. In the interactive REPL this is exactly what happens… each time you complete a block of code and hit enter the block is immediately compiled to bytecode and run. In the non-interactive cases there’s a minor extra step… for efficiency sake the resulting binary bytecode is written to disk as a .pyc file, then it is run. This is strictly a caching step for efficiency… if the same file is run in the future on that machine and the interpreter sees a .pyc file that is the same age or newer than the .py file with the same name it will skip all the parsing and compiling and just run the contents of the .pyc file immediately. If the .pyc file is older or missing it will instead attempt to rebuild it for future runs.
Discussions

Java "Virtual Machine" vs. Python "Interpreter" parlance? - Stack Overflow
It seems rare to read of a Python "virtual machine" while in Java "virtual machine" is used all the time. Both interpret byte codes; why call one a virtual machine and the other an interpreter? More on stackoverflow.com
🌐 stackoverflow.com
The Design and Implementation of the CPython Virtual Machine
So far, it has remained recognisable from one version to the next - someone used to reading (disassembled) CPython 2.7 bytecode would feel right at home reading CPython 3.11 bytecode, for example. But there's no guarantee that it will remain so, and with the faster-cpython project going on, ... More on news.ycombinator.com
🌐 news.ycombinator.com
6
58
September 1, 2024
Selenium on VM?
Have you looked into a Jenkins or Gitlab pipeline/runner? More on reddit.com
🌐 r/selenium
6
5
November 19, 2020
How can I run a Python script 24/7 on Azure?
The easiest way would be to run it as a azure function; couple of options if you want it to be a HTTP end point or run on a timer. Have a go by going to the portal and creating a function app. In the consumption plan they are free of charge for first million executions More on reddit.com
🌐 r/AZURE
7
3
August 23, 2020
🌐
Leanpub
leanpub.com › insidethepythonvirtualmachine › read
Inside The Python Virtual Machine
This books provides a description of the CPython virtual machine and how it executes Python code
🌐
Real Python
realpython.com › ref › glossary › virtual-machine
virtual machine (VM) | Python Glossary – Real Python
The virtual machine (VM) is the component of the Python interpreter that executes your Python code. When you write a Python program, the source code is compiled into bytecode, a low-level set of instructions that is more abstract than machine code.
🌐
Coding Confessions
blog.codingconfessions.com › p › cpython-vm-internals
The Design & Implementation of the CPython Virtual Machine
August 31, 2024 - Now, let’s start diving into the implementation of CPython’s virtual machine. When we execute a new Python program, a lot of things happens before the virtual machine starts to execute the bytecode, such as runtime initialization, parsing and compilation of the code, stack frame setup etc.
🌐
Python documentation
docs.python.org › 3 › library › venv.html
venv — Creation of virtual environments
Virtual environments are created by executing the venv module: ... This creates the target directory (including parent directories as needed) and places a pyvenv.cfg file in it with a home key pointing to the Python installation from which the command was run.
Find elsewhere
🌐
GitHub
github.com › ForceBru › PyVM
GitHub - ForceBru/PyVM: A virtual machine written in Python that executes x86 binaries according to the Intel Software Developer Manual
A virtual machine written in Python that executes x86 binaries according to the Intel Software Developer Manual - ForceBru/PyVM
Starred by 125 users
Forked by 19 users
Languages   Python 49.0% | C 25.5% | Assembly 24.6% | Makefile 0.9% | Python 49.0% | C 25.5% | Assembly 24.6% | Makefile 0.9%
Top answer
1 of 14
214

In this post, "virtual machine" refers to process virtual machines, not to system virtual machines like Qemu or Virtualbox. A process virtual machine is simply a program which provides a general programming environment -- a program which can be programmed.

Java has an interpreter as well as a virtual machine, and Python has a virtual machine as well as an interpreter. The reason "virtual machine" is a more common term in Java and "interpreter" is a more common term in Python has a lot to do with the major difference between the two languages: static typing (Java) vs dynamic typing (Python). In this context, "type" refers to primitive data types -- types which suggest the in-memory storage size of the data. The Java virtual machine has it easy. It requires the programmer to specify the primitive data type of each variable. This provides sufficient information for Java bytecode not only to be interpreted and executed by the Java virtual machine, but even to be compiled into machine instructions. The Python virtual machine is more complex in the sense that it takes on the additional task of pausing before the execution of each operation to determine the primitive data types for each variable or data structure involved in the operation. Python frees the programmer from thinking in terms of primitive data types, and allows operations to be expressed at a higher level. The price of this freedom is performance. "Interpreter" is the preferred term for Python because it has to pause to inspect data types, and also because the comparatively concise syntax of dynamically-typed languages is a good fit for interactive interfaces. There's no technical barrier to building an interactive Java interface, but trying to write any statically-typed code interactively would be tedious, so it just isn't done that way.

In the Java world, the virtual machine steals the show because it runs programs written in a language which can actually be compiled into machine instructions, and the result is speed and resource efficiency. Java bytecode can be executed by the Java virtual machine with performance approaching that of compiled programs, relatively speaking. This is due to the presence of primitive data type information in the bytecode. The Java virtual machine puts Java in a category of its own:

portable interpreted statically-typed language

The next closest thing is LLVM, but LLVM operates at a different level:

portable interpreted assembly language

The term "bytecode" is used in both Java and Python, but not all bytecode is created equal. bytecode is just the generic term for intermediate languages used by compilers/interpreters. Even C compilers like gcc use an intermediate language (or several) to get the job done. Java bytecode contains information about primitive data types, whereas Python bytecode does not. In this respect, the Python (and Bash,Perl,Ruby, etc.) virtual machine truly is fundamentally slower than the Java virtual machine, or rather, it simply has more work to do. It is useful to consider what information is contained in different bytecode formats:

  • llvm: cpu registers
  • Java: primitive data types
  • Python: user-defined types

To draw a real-world analogy: LLVM works with atoms, the Java virtual machine works with molecules, and The Python virtual machine works with materials. Since everything must eventually decompose into subatomic particles (real machine operations), the Python virtual machine has the most complex task.

Intepreters/compilers of statically-typed languages just don't have the same baggage that interpreters/compilers of dynamically-typed languages have. Programmers of statically-typed languages have to take up the slack, for which the payoff is performance. However, just as all nondeterministic functions are secretly deterministic, so are all dynamically-typed languages secretly statically-typed. Performance differences between the two language families should therefore level out around the time Python changes its name to HAL 9000.

The virtual machines of dynamic languages like Python implement some idealized logical machine, and don't necessarily correspond very closely to any real physical hardware. The Java virtual machine, in contrast, is more similar in functionality to a classical C compiler, except that instead of emitting machine instructions, it executes built-in routines. In Python, an integer is a Python object with a bunch of attributes and methods attached to it. In Java, an int is a designated number of bits, usually 32. It's not really a fair comparison. Python integers should really be compared to the Java Integer class. Java's "int" primitive data type can't be compared to anything in the Python language, because the Python language simply lacks this layer of primitives, and so does Python bytecode.

Because Java variables are explicitly typed, one can reasonably expect something like Jython performance to be in the same ballpark as cPython. On the other hand, a Java virtual machine implemented in Python is almost guaranteed to be slower than mud. And don't expect Ruby, Perl, etc., to fare any better. They weren't designed to do that. They were designed for "scripting", which is what programming in a dynamic language is called.

Every operation that takes place in a virtual machine eventually has to hit real hardware. Virtual machines contain pre-compiled routines which are general enough to to execute any combination of logical operations. A virtual machine may not be emitting new machine instructions, but it certainly is executing its own routines over and over in arbirtrarily complex sequences. The Java virtual machine, the Python virtual machine, and all the other general-purpose virtual machines out there are equal in the sense that they can be coaxed into performing any logic you can dream up, but they are different in terms of what tasks they take on, and what tasks they leave to the programmer.

Psyco for Python is not a full Python virtual machine, but a just-in-time compiler that hijacks the regular Python virtual machine at points it thinks it can compile a few lines of code -- mainly loops where it thinks the primitive type of some variable will remain constant even if the value is changing with each iteration. In that case, it can forego some of the incessent type determination of the regular virtual machine. You have to be a little careful, though, lest you pull the type out from under Psyco's feet. Pysco, however, usually knows to just fall back to the regular virtual machine if it isn't completely confident the type won't change.

The moral of the story is that primitive data type information is really helpful to a compiler/virtual machine.

Finally, to put it all in perspective consider this: a Python program executed by a Python interpreter/virtual machine implemented in Java running on a Java interpreter/virtual machine implemented in LLVM running in a qemu virtual machine running on an iPhone.

permalink

2 of 14
149

A virtual machine is a virtual computing environment with a specific set of atomic well defined instructions that are supported independent of any specific language and it is generally thought of as a sandbox unto itself. The VM is analogous to an instruction set of a specific CPU and tends to work at a more fundamental level with very basic building blocks of such instructions (or byte codes) that are independent of the next. An instruction executes deterministically based only on the current state of the virtual machine and does not depend on information elsewhere in the instruction stream at that point in time.

An interpreter on the other hand is more sophisticated in that it is tailored to parse a stream of some syntax that is of a specific language and of a specific grammer that must be decoded in the context of the surrounding tokens. You can't look at each byte or even each line in isolation and know exactly what to do next. The tokens in the language can't be taken in isolation like they can relative to the instructions (byte codes) of a VM.

A Java compiler converts Java language into a byte-code stream no different than a C compiler converts C Language programs into assembly code. An interpreter on the other hand doesn't really convert the program into any well defined intermediate form, it just takes the program actions as a matter of the process of interpreting the source.

Another test of the difference between a VM and an interpreter is whether you think of it as being language independent. What we know as the Java VM is not really Java specific. You could make a compiler from other languages that result in byte codes that can be run on the JVM. On the other hand, I don't think we would really think of "compiling" some other language other than Python into Python for interpretation by the Python interpreter.

Because of the sophistication of the interpretation process, this can be a relatively slow process....specifically parsing and identifying the language tokens, etc. and understanding the context of the source to be able to undertake the execution process within the interpreter. To help accelerate such interpreted languages, this is where we can define intermediate forms of pre-parsed, pre-tokenized source code that is more readily directly interpreted. This sort of binary form is still interpreted at execution time, it is just starting from a much less human readable form to improve performance. However, the logic executing that form is not a virtual machine, because those codes still can't be taken in isolation - the context of the surrounding tokens still matter, they are just now in a different more computer efficient form.

🌐
GitHub
github.com › topics › python-virtual-machine
python-virtual-machine · GitHub Topics · GitHub
python documentation docs gplv3 ... ... 🐍️📦️🐍️🌐️ The official source repository for the website of the PYVM project, an experimental virtual, machine for Python....
🌐
Kentdlee
kentdlee.github.io › CoCoPages
CoCo - A Python Virtual Machine
We cannot provide a description for this page right now
🌐
Hacker News
news.ycombinator.com › item
The Design and Implementation of the CPython Virtual Machine | Hacker News
September 1, 2024 - So far, it has remained recognisable from one version to the next - someone used to reading (disassembled) CPython 2.7 bytecode would feel right at home reading CPython 3.11 bytecode, for example. But there's no guarantee that it will remain so, and with the faster-cpython project going on, ...
🌐
DEV Community
dev.to › imsushant12 › understanding-python-bytecode-and-the-virtual-machine-for-better-development-55a9
Understanding Python Bytecode and the Virtual Machine for Better Development - DEV Community
August 22, 2024 - Our main goal today is to understand what happens behind the scenes when we write and execute Python code. Python is an interpreted language, but it also involves a compilation step where your Python code (.py) is compiled into bytecode (.pyc). This bytecode is then executed by the Python Virtual Machine (PVM).
🌐
DevOpsSchool.com
devopsschool.com › home › python tutorials: python virtual machine
Python Tutorials: Python Virtual Machine - DevOpsSchool.com
July 24, 2022 - Python Virtual Machine (PVM) is a program which provides programming environment. The role of PVM is to convert the byte code instructions into machine code so the computer can execute those machine code instructions and display the output.
🌐
Leanpub
leanpub.com › insidethepythonvirtualmachine
Inside The Python Virtual Machine [Leanpub PDF/iPad/Kindle]
You know how to program in Python but are interested in what goes on under the covers of the interpreter? Well, fasten your seat-belts as this book will take you on a tour of the virtual machine that runs your Python code.
🌐
Medium
caiocozza-art.medium.com › a-quick-overview-of-the-python-virtual-machine-pt-1-315e74c036f4
A quick overview of the Python Virtual Machine — Pt. 1 | by Caio Cozza | Medium
September 20, 2019 - Python is just a C program. This program is also called Python Virtual Machine, and this virtual machine has several tasks to do before your Python code is indeed executed. The virtual machine cannot execute a Python code as you write in your ...
🌐
Hashnode
pythonvirsualmachine.hashnode.dev › python-virtual-machine-the-heart-of-pythons-runtime
The Heart of Python's Runtime. - Python Virtual Machine
December 10, 2024 - The Python Virtual Machine (PVM) is an interpreter that executes Python bytecode. It acts as a runtime engine, converting platform-independent bytecode into machine-level instructions that your hardware can understand.
🌐
Tenthousandmeters
tenthousandmeters.com › blog › python-behind-the-scenes-1-how-the-cpython-vm-works
Python behind the scenes #1: how the CPython VM works
August 31, 2020 - We've seen that it works in three stages: ... The part of the interpreter that is responsible for the bytecode execution is called a virtual machine. The CPython VM has several particularly important concepts: code objects, frame objects, thread states, interpreter states and the runtime.