Questions about "compiled" Python (beginner)

Discussion in 'Python' started by HoneyMonster, Jan 29, 2012.

  1. HoneyMonster

    HoneyMonster Guest

    I am new to Python (Python 2.7 on Linux). Research indicates that:

    a) "Compiling" Python modules into intermediate bytecode marginally
    improves load time.

    b) The Python interpreter will use an already-prepared .pyc file if one
    exists in the same directory as the .py.

    That then, is presumably why for every .py file in my site-packages
    directory there is a corresponding .pyc file.

    Question 1: What then, are the .pyo files? I note that many of them are
    identical to the .pyc, but that some differ.

    Question 2: What happens if the .py file is changed and the .pyc is thus
    made obsolete. Does the interpreter ignore the .pyc? If so, how does it
    know? By the timestamp?

    Thanks.
    HoneyMonster, Jan 29, 2012
    #1
    1. Advertising

  2. HoneyMonster

    Terry Reedy Guest

    On 1/29/2012 12:57 PM, HoneyMonster wrote:
    > I am new to Python (Python 2.7 on Linux). Research indicates that:
    >
    > a) "Compiling" Python modules into intermediate bytecode marginally
    > improves load time.


    The improvement is larger the larger the file. You may notice that .pyc
    files are only created when a file is imported, not when it is run
    directly.

    > b) The Python interpreter will use an already-prepared .pyc file if one
    > exists in the same directory as the .py.
    >
    > That then, is presumably why for every .py file in my site-packages
    > directory there is a corresponding .pyc file.


    In 3.2+, .pyc files are tucked away in a __pycache__ directory, with a
    version indicator added to the names so one directory can be used with
    more than one version of python.

    > Question 1: What then, are the .pyo files? I note that many of them are
    > identical to the .pyc, but that some differ.


    They are created when imported into python started with -O (optimize).
    That mainly deletes assertions and maybe something else.
    >
    > Question 2: What happens if the .py file is changed and the .pyc is thus
    > made obsolete. Does the interpreter ignore the .pyc? If so, how does it
    > know? By the timestamp?


    Yes. Yes.

    --
    Terry Jan Reedy
    Terry Reedy, Jan 29, 2012
    #2
    1. Advertising

  3. Cousin Stanley, Jan 29, 2012
    #3
  4. HoneyMonster

    HoneyMonster Guest

    On Sun, 29 Jan 2012 14:01:01 -0500, Terry Reedy wrote:

    > On 1/29/2012 12:57 PM, HoneyMonster wrote:
    >> I am new to Python (Python 2.7 on Linux). Research indicates that:
    >>
    >> a) "Compiling" Python modules into intermediate bytecode marginally
    >> improves load time.

    >
    > The improvement is larger the larger the file. You may notice that .pyc
    > files are only created when a file is imported, not when it is run
    > directly.
    >
    >> b) The Python interpreter will use an already-prepared .pyc file if one
    >> exists in the same directory as the .py.
    >>
    >> That then, is presumably why for every .py file in my site-packages
    >> directory there is a corresponding .pyc file.

    >
    > In 3.2+, .pyc files are tucked away in a __pycache__ directory, with a
    > version indicator added to the names so one directory can be used with
    > more than one version of python.
    >
    >> Question 1: What then, are the .pyo files? I note that many of them are
    >> identical to the .pyc, but that some differ.

    >
    > They are created when imported into python started with -O (optimize).
    > That mainly deletes assertions and maybe something else.
    >>
    >> Question 2: What happens if the .py file is changed and the .pyc is
    >> thus made obsolete. Does the interpreter ignore the .pyc? If so, how
    >> does it know? By the timestamp?

    >
    > Yes. Yes.


    Thanks, Terry and Cousin Stanley for the clear explanation and useful URL.
    HoneyMonster, Jan 30, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    562
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Rensjuh
    Replies:
    7
    Views:
    954
    Mabden
    Sep 2, 2004
  3. Klaus Schneider
    Replies:
    1
    Views:
    531
    Rolf Magnus
    Dec 2, 2004
  4. lander
    Replies:
    5
    Views:
    582
    bruce barker
    Mar 5, 2008
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    203
    7stud --
    Apr 12, 2011
Loading...

Share This Page