39

Is Python strictly interpreted at run time, or can it be used to develop programs that run as background applications (like a Java app or C program)?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Geuis
  • 41,122
  • 56
  • 157
  • 219
  • Related posts - [Is Python interpreted, or compiled, or both?](https://stackoverflow.com/q/6889747/465053) & [How does the Python Runtime actually work?](https://softwareengineering.stackexchange.com/q/313254/236257) – RBT Jul 11 '18 at 08:17

6 Answers6

97

As the varied responses will tell you, the line between interpreted and compiled is no longer as clear as it was when such terms were coined. In fact, it's also something of a mistake to consider languages as being either interpreted or compiled, as different implementations of languages may do different things. These days you can find both C interpreters and Javascript compilers.

Even when looking at an implementation, things still aren't clear-cut. There are layers of interpretation. Here are a few of the gradations between interpreted and compiled:

  1. Pure interpretation. Pretty much what it says on the tin. Read a line of source and immediately do what it says. This isn't actually done by many production languages - pretty much just things like shell scripts.

  2. Tokenisation + interpretation. A trivial optimization on the above. Rather than interpret each line from scratch, it's first tokenized (that is, rather than seeing a string like "print 52 + x", it's translated into a stream of tokens (eg. [PRINT_STATEMENT, INTEGER(52), PLUS_SIGN, IDENTIFIER('x')] ) to avoid repeatedly performing that state of interpretation. Many versions of basic worked this way.

  3. Bytecode compilation. This is the approach taken by languages like Java and C# (though see below). The code is transformed into instructions for a "virtual machine". These instructions are then interpreted. This is also the approach taken by python (or at least cpython, the most common implementation.) The Jython and Ironpython implementations also take this approach, but compile to the bytecode for the Java and C# virtual machines respectively.

  4. Bytecode + Just in Time compilation. As above, but rather than interpreting the bytecodes, the code that would be performed is compiled from the bytecode at the point of execution and then run. In some cases, this can actually outperform native compilation, as it is free to perform runtime analysis on the code, and can use specific features of the current processor (while static compilation may need to compile for a lowest common denominator CPU). Later versions of Java, and C# use this approach. Psyco performs this for python.

  5. Native machine-code compilation. The code is compiled to the machine code of the target system. You may think we've now completely eliminated interpretation, but even here there are subtleties. Some machine code instructions are not actually directly implemented in hardware, but are in fact implemented via microcode - even machine code is sometimes interpreted!

Utsav T
  • 1,515
  • 2
  • 24
  • 42
Brian
  • 116,865
  • 28
  • 107
  • 112
54

There's multiple questions here:

  1. No, Python is not interpreted. The standard implementation compiles to bytecode, and then executes in a virtual machine. Many modern JavaScript engines also do this.
  2. Regardless of implementation (interpreter, VM, machine code), anything you want can run in the background. You can run shell scripts in the background, if you want.
John Millikin
  • 197,344
  • 39
  • 212
  • 226
  • Many thanks for such a quick answer. That also puts a lot of other things into context I've been reading about Python. – Geuis Apr 13 '09 at 23:29
  • 28
    The "not interpreted" is an overstatement. It's not interpreted as source. The Python VM clearly interprets the bytecode. It is not compiled into machine-specific binaries, so it's interpreted at some level. – S.Lott Apr 14 '09 at 10:00
  • Lott: by that measure, almost every modern language is interpreted, and the word ceases to have a useful meaning. – John Millikin Apr 14 '09 at 16:12
  • 6
    I think the difference lies in whether the interpretation is done by hardware (the CPU) or software (the Python VM). I consider Python to be interpreted. – Hannes Ovrén Apr 14 '09 at 20:27
  • 2
    kigurai: Then how do you differentiate between an implementation that uses a VM, and one that uses an interpreter? There's already a perfectly good and accurate word for both ("byte-compiled", "interpreted") -- lets use them, instead of adding new meanings. – John Millikin Apr 14 '09 at 20:44
  • If Python is interpreted then Java is interpreted too. – Oli Apr 15 '09 at 07:12
  • If Python /java is not interpreted, would you it's compiled ? Not sure it makes sense. It's closer to interpreted language than to compiled one, I think. – Bite code Oct 31 '09 at 13:43
  • 13
    "No, Python is not interpreted." Someone better tell the people at python.org to change their meta description! "Home page for Python, an interpreted, interactive, object-oriented, extensible programming language." – Honest Abe Aug 17 '12 at 06:20
26

Technically, Python is compiled to bytecode and then interpreted in a virtual machine. If the Python compiler is able to write out the bytecode into a .pyc file, it will (usually) do so.

On the other hand, there's no explicit compilation step in Python as there is with Java or C. From the point of view of the developer, it looks like Python is just interpreting the .py file directly. Plus, Python offers an interactive prompt where you can type Python statements and have them executed immediately. So the workflow in Python is much more similar to that of an interpreted language than that of a compiled language. To me (and a lot of other developers, I suppose), that distinction of workflow is more important than whether there's an intermediate bytecode step or not.

Honest Abe
  • 8,430
  • 4
  • 49
  • 64
David Z
  • 128,184
  • 27
  • 255
  • 279
  • For a language to be "interpreted" implies the use of an interpreter in its implementation. It's a purely performance-related issue. Whether or not the language can be used in an interactive prompt is irrelevant: Haskell and C# can both be used in an REPL, and are undeniably not interpreted. – John Millikin Apr 13 '09 at 23:34
4

Python is an interpreted language but it is the bytecode which is interpreted at run time. There are also many tools out there that can assist you in making your programs run as a windows service / UNIX daemon.

John T
  • 23,735
  • 11
  • 56
  • 82
  • +1 to even out as this is a good answer. You're not wrong: there's a difference between a purely compiled language and python's byte-compilation as python's pyc are generated, interpretedly, when the .py is accessed by the runtime. That's not interpreted like PHP, but it's also not compiled like – Jarret Hardie Apr 13 '09 at 23:24
  • If Python is interpreted, then so are Java and C#. Just because the compiler will be invoked automatically doesn't mean it's not compiled. I'm also mystified about what "purely compiled" means. – John Millikin Apr 13 '09 at 23:25
  • I mean there's no linker. In that sense (see Joel On Software http://www.joelonsoftware.com/articles/PleaseLinker.html), you're right... python shares much with java and C#. Still, there's no explicit "pythoncc" step, which makes the semantics of compile/interpret non-straightforward, IMO. – Jarret Hardie Apr 13 '09 at 23:32
  • 2
    The difference between compilation and interpretation is still straightforward. An interpreter traverses the parsed representation at runtime and evaluates nodes. A compiler converts the representation into a separate format, which is evaluated (either by hardware or a virtual machine). – John Millikin Apr 13 '09 at 23:39
  • Well, somebody had better tell Guido, because his resume (http://www.python.org/~guido/Resume.html) explicitly calls Python interpreted. I feel Python is compiled. Still, many will say that ANY VM-based runtime is interpreted, and as a translation between VM-code and the OS/hardware, it is. – Jarret Hardie Apr 13 '09 at 23:44
  • It's possible that, at the time, Python *was* interpreted. Interpreters are easier to write than compilers -- for example, until recently, Ruby was interpreted. – John Millikin Apr 13 '09 at 23:52
  • No disagreement from me there. – Jarret Hardie Apr 13 '09 at 23:57
  • @Jarret--what's this strange distinction you make between "VM" and "hardware". The VM *is* the hardware, isn't it???? :-) – James Schek Apr 14 '09 at 00:25
2

Yes, Python is interpreted, but you can also run them as long-running applications.

user90052
  • 2,550
  • 3
  • 16
  • 7
2

Yes, it's interpreted, its main implementation compiles bytecode first and then runs it though (kind of if you took a java source and the JVM compiled it before running it). Still, you can run your application in background. Actually, you can run pretty much anything in background.

Phrodo_00
  • 49
  • 2