Newbie: Compiling a Python Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • giovannifreda
    New Member
    • Mar 2007
    • 1

    #1

    Newbie: Compiling a Python Script

    I am new to python and I am wondering if there is a way to take a python script and turn it into an object of some kind such that the python system doesn't have to take the script and compile it down to python code every time i try to run the script in my program.

    I have a lot of scripts and they are getting used a lot so this is a performance question really.

    If anybody knows how to take the .py file and "compile" it into byte code that i can pass to python directly I would appreciate it. I have seen people talk about compiling the script but it seems like it is a pre-run time operation and I need something that is during run time because my scripts can change during run time if the user wants.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by giovannifreda
    I am new to python and I am wondering if there is a way to take a python script and turn it into an object of some kind such that the python system doesn't have to take the script and compile it down to python code every time i try to run the script in my program.

    I have a lot of scripts and they are getting used a lot so this is a performance question really.

    If anybody knows how to take the .py file and "compile" it into byte code that i can pass to python directly I would appreciate it. I have seen people talk about compiling the script but it seems like it is a pre-run time operation and I need something that is during run time because my scripts can change during run time if the user wants.
    Ok, let's see if I can make this clear.
    Modules (scripts) are only compiled after they have been changed (or created the first time, of course). It is possible to treat modules as objects, but the goal you are after is taken care of automaticly. Any time your script does
    Code:
    import moduleA
    python checks to see if has been modified and compiles it if it has been. You can prove this by removing the .py file so that moduleA.pyc is all that's left in the directory that you are working in. The import will still work.
    Have fun and keep posting,
    Barton
    PS, welcome to the Python Forum on TheScripts.com.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      Ok, let's see if I can make this clear.
      Modules (scripts) are only compiled after they have been changed (or created the first time, of course). It is possible to treat modules as objects, but the goal you are after is taken care of automaticly. Any time your script does
      Code:
      import moduleA
      python checks to see if has been modified and compiles it if it has been. You can prove this by removing the .py file so that moduleA.pyc is all that's left in the directory that you are working in. The import will still work.
      Have fun and keep posting,
      Barton
      PS, welcome to the Python Forum on TheScripts.com.
      I've been clocking an app packed into an exe with py2exe vs a many-module version of the same program. Clock speeds are the same in both cases.

      Comment

      Working...