Not all .PY files are compiled to .PYC during execution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony C

    Not all .PY files are compiled to .PYC during execution

    After using Python for just over a year now, I've noticed something
    for the first time.

    I've written an application in one .PY file, and a class definition in
    another.PY file. (The application instantiates one instance of the
    class.)

    When I run my application as in

    python myapp.py

    the file which contains the class definition (class.py), is compiled
    to a .PYC file, but the application (myapp.py) is not.

    Why is the application file not compiled to .PYC ?

    thanks
  • Erik Max Francis

    #2
    Re: Not all .PY files are compiled to .PYC during execution

    Tony C wrote:[color=blue]
    >
    > Why is the application file not compiled to .PYC ?[/color]

    When you import a .py file, a .pyc is created. When you run it
    directly, it isn't. The thinking is probably that something that's a
    module is less likely to change than something that's a script. I don't
    really think there's much stock to take in the difference; it's just the
    way it is.

    --
    __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ Love is when you wake up in the morning and have a big smile.
    -- Anggun

    Comment

    • Robert M. Emmons

      #3
      Re: Not all .PY files are compiled to .PYC during execution

      Tony C wrote:[color=blue]
      > After using Python for just over a year now, I've noticed something
      > for the first time.
      >
      > I've written an application in one .PY file, and a class definition in
      > another.PY file. (The application instantiates one instance of the
      > class.)
      >
      > When I run my application as in
      >
      > python myapp.py
      >
      > the file which contains the class definition (class.py), is compiled
      > to a .PYC file, but the application (myapp.py) is not.
      >
      > Why is the application file not compiled to .PYC ?
      >
      > thanks[/color]
      It is generally recommended that the main program be small and they you
      you put most of your code in modules if you have a large program and
      many modules.

      I don't know officially why python works this way, but perhaps it's to
      keep launching simple.

      My speculation as to why it works this way is because when you create a
      python script -- you generally are creating a command or program. You
      don't want to have to different file names for this program --
      especially if it's in the path. You want the file to have a single
      launch point and this has to be source if your going to keep things simple.

      Rob

      Comment

      Working...