Python Forum
Question about how python being interpreted.
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about how python being interpreted.
#1
Hi all,

My question is a bit general, but for some reason I couldn't find any answer for it online.
I know that python is mostly an interpreted language.
Usually an interpreted language (as far as I know) is running one line at a time, each line being translated into machine code and then moving on to the next line...

I also know that you can use py_compile module in order to first compile your code into byte-code and then run it (being interpreted into machine code).

My question is as follows, does python only compile into byte-code when i specifically ask for it using py_compile?
or if I just run a python file (from cmd or even pycharm) it will also convert my code into byte-code first?

Thanks in advance,
Nirel.
Reply
#2
You shouldn't need to use the py_compile module at all, unless you're doing something wacky like parsing dynamically generated python and running it (like, if you were a website that let other people run python code), and even then there's better ways of doing it (like putting the code in a virtual environment, so it can't accidentally break the machine).

Simply running your script through the interpreter will create a byte-code compiled version of the script. That byte code is stored in a few different places depending on what version you're running. Older versions of python put them right next to your source, with a .pyc extension. Newer versions of python use a __pycache__ directory, and embed the version of python in the file name (so places like linux/macs that run multiple versions of python can each run fine).

You pretty much don't need to worry about it, or if it's being used. One of the first things the interpreter does when you run it is check if there's a cached version of your script, and whether that cached version is out of date. The caching should be considered an implementation detail... and I don't think all versions of python do it (like IronPython or pypy or jython).
Reply
#3
is this question going to lead to is python slower or faster do X thing?
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,462 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,537 Aug-25-2020, 03:43 AM
Last Post: micseydel
  timedelta object cannot be interpreted as integer palladium 5 13,294 Jan-30-2020, 02:54 PM
Last Post: palladium

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020