[Tutor] Sun says: Don't use Java, use Python!

Jeff Shannon jeff@ccvcorp.com
Mon Feb 10 16:32:05 2003


Paul Tremblay wrote:

>On Mon, Feb 10, 2003 at 05:25:04PM -0000, alan.gauld@bt.com wrote:
>  
>
>>>...The key difference is that Python is a scripting language. 
>>>This means there is no compilation to byte code ....It has to 
>>>perform syntax checks and it must parse the ascii text 
>>>      
>>>
>>Interesting but only partially right since most of the time for 
>>production code Python will already have compiled its modules to 
>>byte code and only the main module remains in ascii text.
>>    
>>
>So doesn't this mean that if you are planning to distribute a Python
>script (as I am), that you should distribute it as a module? I hadn't
>even considered that modules are faster than scripts before reading this
>thread.
>  
>

That depends on the script.  Remember, the term 'module', when applied 
to Python files, is simply a statement that the file is used via 
'import' in some other file, so distributing a script as a module 
implies that you have a script file that imports that module and runs 
some function(s) from it.  

If it's a short, quick script, then it's probably not going to make 
enough of a difference to worry about.  The simplicity of distributing a 
single file would be more valuable than the speed gain that you'd get 
from having the bytecode precompiled.  (Precompilation only affects 
startup speed, *not* actual running speed.)

If it's a longer script, then you might get enough of a time savings to 
make it worthwhile having a second "driver" script, that does nothing 
but import your main file as a module and run a function from it 
('import mymodule; mymodule.run()').  However, if your program is big 
enough that the startup cost is significant, then odds are that it's big 
enough that it should be split into more than one module anyhow.  Using 
several small, functionally discrete modules makes a program easier to 
understand and maintain than having one or two big, bulky, 
everything-but-the-kitchen-sink modules.  So in this case, it's probably 
best to have three or four or six (or whatever number of subunits your 
program logically breaks down into) separate modules, and organize all 
of those into a package.  This involves not much more than putting them 
in their own directory and adding a blank __init__.py file, and some 
slight changes to your "driver" script ('from mypackage import main; 
main.run()').

Depending on your distribution requirements, it may or may not be worth 
the effort to set up a distutils script to install your package on new 
machines.  If it's a pure Python package, then all distutils would be 
doing is copying it to a subdirectory of 
[Python-home]\Lib\site-packages, which is easy enough to do manually...

Jeff Shannon
Technician/Programmer
Credit International