[Python-bugs-list] [ python-Bugs-570640 ] .PYO files not imported unless -OO used

noreply@sourceforge.net noreply@sourceforge.net
Wed, 24 Jul 2002 09:01:29 -0700


Bugs item #570640, was opened at 2002-06-18 15:31
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=570640&group_id=5470

Category: Parser/Compiler
Group: Python 2.1.2
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Jeff Sasmor (jeffsasmor)
Assigned to: Nobody/Anonymous (nobody)
Summary: .PYO files not imported unless -OO used

Initial Comment:
If you execute a program where all the files/modules 
are .pyo, that is, compiled while the interpreter had been 
started with the -OO command-line option (and presumably -
O as well), then imports fail unless the interpreter had been 
started with -OO (-O?) during that run of the program.

Thus, if you want to distribute a program in its smallest form 
where you might run a 'build' script that uses the compileall
built-in to create a distribution fileset that comprises 
only .pyo files, you'd obviously need to run that script with 
the Python interpreter in -OO 'mode'.  

When you you try to run the target program from the 
distribution fileset, you are required to start the interpreter 
in -OO mode or all imports fail since the interpreter does not 
search for that extension unless -OO has been used. One 
workaround is to rename all the .pyo files to .pyc files, but 
this is somewhat silly....  This sitution is undocumented 
AFAIK. 

Additionally, it would be nice to be able to switch this 
optimize mode on/off programmatically.  In the app I am 
working on (an 'IDE' for Python) I store code objects 
(created by using the compile built-in)  in the ZODB.  If I 
have to run the interpreter in -OO mode then I lose debug 
information that I'd like to keep. Hence it would be nice to 
be able to turn this mode on and off.

----------------------------------------------------------------------

>Comment By: Jeremy Hylton (jhylton)
Date: 2002-07-24 16:01

Message:
Logged In: YES 
user_id=31392

I'm not sure I understand what you want to see changed.

If you only distributed .pyo files, then the python must
indeed by invoked with -O to be able to use these files.  In
general, Python programs are distributed as .py files that
can be used with or without -O.  If you want to distribute
without source but work in either case, distribute the .pyc
and .pyo.

The compiled code is different in .pyc and .pyo files, which
is why there are two different files.  The size of these
files is small compared to the size of most disk drives, so
I don't see why you would worry about a factor of 2 in size
of total distribution.

The -O flag affects the way the code is compiled.  assert
stmts, for example, generate no compiled code in -O mode. 
It isn't practical to turn this on and off at runtime.  In
projects where we store code in ZODB, we use a dictionary
keyed on __debug__ (and Python version) to make sure the
compiled code object is compatible with the current interpreter.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=570640&group_id=5470